笔试强训(十三)

一.牛牛冲钻五

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;
}
相关推荐
倒头就睡的小比特3 天前
算法竞赛C++常用的STL
c++·算法
weilx12343 天前
C++笔记-文件IO-<fcntl.h>
c++
小羊没烦恼!3 天前
初探性能优化——2个月到4小时的性能提升
java·开发语言·windows·算法·c#
伞伞悦读3 天前
【第38期】Python 模块与包详解:import、from、模块搜索路径、包结构和 __init__
开发语言·python
猎头南楼3 天前
知识社区推荐系统实践:新用户冷启动与长短期兴趣建模的挑战 资深推荐算法工程师
人工智能·深度学习·算法·机器学习
Smileyqp沛沛3 天前
前端?C++ ?较大差异基础罗列
c++·基础·前端转c++
C语言小火车3 天前
C/C++ 为什么需要编译器?
开发语言·c++
旖旎夜光3 天前
力控面试题 01.01: 判定字符是否唯一(位运算) —— 题解
c++·学习·算法·leetcode·力控
wzdark3 天前
大规模并行计算中的负载均衡算法研究4
算法
吞下星星的少年·-·3 天前
C++ 萌新语法入门篇
c++·算法比赛