笔试强训(十三)

一.牛牛冲钻五

https://ac.nowcoder.com/acm/problem/227309

cpp 复制代码
#include <iostream>
#include <string.h>

using namespace std;


void slove()
{
    int n,k;
    cin >> n >> k;
    string s;
    cin >> s;
    int ans = 0,len = 0;
    for(int i = 0;i < n; i++)
    {
        if(s[i] == 'W')
        {
            len ++;
            if(len >= 3)
            {
                ans += k;
            }
            else
            {
                ans += 1;
            }
        }
        else
        {
            len = 0;
            ans -= 1;
        }
    }
    cout << ans << endl;
}

int main() {
    int n;
    cin >> n;
    while(n--)
    {
        slove();
    }
    return 0;
}
// 64 位输出请用 printf("%lld")

二.最长无重复子数组

https://www.nowcoder.com/practice/b56799ebfd684fb394bd315e89324fb4?tpId=196&tqId=37149&ru=/exam/oj

cpp 复制代码
#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;


class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param arr int整型vector the array
     * @return int整型
     */
    int maxLength(vector<int>& arr) {
        // write code here
        unordered_map<int,int> mp;
        int n = arr.size();
        int ans = 0;
        int l = 0,r = 0;
        while(r < n)
        {
            mp[arr[r]]++;
            if(mp[arr[r]] >= 2)
            {
                while(mp[arr[r]] >= 2)
                {
                    mp[arr[l++]]--;
                }
                ans = max(ans,r - l + 1);
            }
            else
            {
                ans = max(ans,r - l + 1);
            }
            r++;
        }
        return ans;
    }
};

三.重排字符串

https://ac.nowcoder.com/acm/problem/222104

cpp 复制代码
#include <iostream>
#include <string.h>
using namespace std;

const int N = 1e5 + 10;
const int M = 30;
char a[N];
int sum[M];

int main()
{
    int n;
    cin >> n;
    string s;
    cin >> s;
    int mmax = 0,mmax_index = 0;
    for(int i = 0;i < n; i++)
    {
        sum[s[i] - 'a']++;
        if(sum[s[i] - 'a'] > mmax)
        {
            mmax = sum[s[i] - 'a'];
            mmax_index = s[i] - 'a';
        }
    }

    if(mmax > n - mmax + 1)
    {
        cout << "no";
    }
    else
    {
        cout << "yes" << endl;
        int begin = 0;
        while(mmax)
        {
            a[begin] = mmax_index + 'a';
            mmax--;
            begin+=2;
        } 
        for(int i = 0;i < 26; i++)
        {
            if(i != mmax_index && sum[i])
            {
                while(sum[i]--)
                {
                    if(begin >= n)
                    {
                        begin = 1;
                    }
                    a[begin] = i + 'a';
                    begin+=2;
                }
            }
        }
        for(int i = 0;i < n;i++)
        {
            cout << a[i] ;
        }
        cout << endl;
    }


    return 0;
}
相关推荐
叼烟扛炮4 分钟前
C++ 知识点02 输入输出
开发语言·c++·算法·输入输出
qcx236 分钟前
深度解析Deepseek V4:1M 上下文不是军备竞赛,是养 Agent 的人才知道的痛
java·开发语言
小此方9 分钟前
Re:思考·重建·记录 现代C++ C++11篇(六) 从 shared_ptr 到 weak_ptr:起底智能指针的引用计数与循环引用之痛
开发语言·c++·c++11·现代c++
字节高级特工9 分钟前
MySQL数据库基础与实战指南
数据库·c++·人工智能·后端·mysql·adb
郝学胜-神的一滴11 分钟前
中级OpenGL教程 004:为几何体注入法线灵魂
c++·unity·游戏引擎·godot·图形渲染·opengl·unreal
晨非辰11 分钟前
吃透C++两大默认成员函数:const成员函数、 & 取地址运算符重载
java·大数据·开发语言·c++·人工智能·后端·面试
我滴老baby14 分钟前
多智能体协作系统设计当AI学会团队合作效率翻十倍
android·开发语言·人工智能
落雪寒窗-16 分钟前
Python进阶核心路线(工程向)
开发语言·python
humcomm16 分钟前
全栈开发技术栈的最新进展(2026年视角)
开发语言·架构
代码地平线32 分钟前
【数据结构】二叉树详解:全代码逐行解析+6道LeetCode高频OJ题图解
数据结构·算法·leetcode