笔试强训(十三)

一.牛牛冲钻五

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;
}
相关推荐
qq_4798754337 分钟前
C++ 网络编程中的 Protobuf 消息分发 (Dispatcher) 设计模式
网络·c++·设计模式
前端小L37 分钟前
回溯算法专题(八):精细化切割——还原合法的「IP 地址」
数据结构·算法
Tandy12356_38 分钟前
手写TCP/IP协议——IP层输出处理
c语言·网络·c++·tcp/ip·计算机网络
博语小屋41 分钟前
实现简单日志
linux·服务器·数据库·c++
Y1rong4 小时前
C++ QT之记事本
开发语言·qt
Hcoco_me7 小时前
大模型面试题17:PCA算法详解及入门实操
算法
跨境卫士苏苏7 小时前
亚马逊AI广告革命:告别“猜心”,迎接“共创”时代
大数据·人工智能·算法·亚马逊·防关联
ZouZou老师7 小时前
C++设计模式之装饰器模式:以家具生产为例
c++·设计模式·装饰器模式
ZouZou老师7 小时前
C++设计模式之桥接模式:以家具生产为例
c++·设计模式·桥接模式
diegoXie7 小时前
Python / R 向量顺序分割与跨步分割
开发语言·python·r语言