笔试强训day13



牛牛冲钻五

#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>

using namespace std;
int n, k;
int hx[26]{};

void solve()
{
	cin >> n >> k;
	string s;
	cin >> s;
	int ans = 0;
	for (auto& c : s)
	{
		hx[c - 'A']++;
		if (c == 'W')
		{
			if (hx['W' - 'A'] >= 3)ans += k;
			else ans += 1;
		}
		else {
			hx['W' - 'A'] = 0;
			ans -= 1;
		}
	}
	cout << ans << '\n';
    memset(hx,0,sizeof hx);
}

int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);
	int t = 1;
	cin >> t;
	while (t--)solve();
	return 0;
}


最长无重复子数组

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


重排字符串

#include <iostream>
using namespace std;
const int N = 100010;
int n;
char s[N];
char ret[N];
int main()
{
    cin >> n >> s;

    int hash[26] = { 0 }; // 统计每个字符的频次
    int maxIndex, maxCount = 0;
    for (int i = 0; i < n; i++)
    {
        if (maxCount < ++hash[s[i] - 'a'])
        {
            maxCount = hash[s[i] - 'a'];
            maxIndex = s[i] - 'a';
        }
    }

    if (maxCount > (n + 1) / 2) cout << "no" << endl;
    else
    {
        cout << "yes" << endl;
        int index = 0;
        // 先去摆放出现次数最多的
        while (maxCount--)
        {
            ret[index] = maxIndex + 'a';
            index += 2;
        }
        // 处理剩下的
        for (int i = 0; i < 26; i++)
        {
            if (hash[i] && i != maxIndex)
            {
                while (hash[i]--)
                {
                    if (index >= n) index = 1;
                    ret[index] = i + 'a';
                    index += 2;
                }
            }
        }
        // 打印结果
        for (int i = 0; i < n; i++) cout << ret[i];
        cout << endl;
    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值