笔试强训(十三)

一.牛牛冲钻五

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;
}
相关推荐
dsyyyyy110110 分钟前
JavaScript变量
开发语言·javascript·ecmascript
‎ദ്ദിᵔ.˛.ᵔ₎14 分钟前
双指针、滑动窗口、前缀和、二分查找 算法
算法
顾北顾28 分钟前
多头注意力机制
人工智能·深度学习·算法
H1785350909631 分钟前
SolidWorks_基于草图的实体特征20_特征错误排查
算法·3d建模·solidworks
hujinyuan2016042 分钟前
2025年12月中国电子学会青少年机器人技术等级考试试卷(二级) 真题+答案
人工智能·算法·机器人
玖玥拾1 小时前
C/C++ 基础笔记(十三)继承
c语言·c++·继承
z落落1 小时前
C#WinForm 窗体切换与窗体传值(登录跳转案例)+WinForm 窗体传值(从上往下传、从下往上传)
开发语言·windows·c#
allway21 小时前
How to Echo Multiline to a File in Bash [3 Methods]
开发语言·chrome·bash
weixin_462446231 小时前
手把手教你用 Bash 脚本自动更新 /etc/hosts —— 自动绑定网卡 IP 与节点名
开发语言·tcp/ip·bash
一个梦醒了1 小时前
安装git bash选项推荐
开发语言·git·bash