牛牛冲钻五
题目链接:A-牛牛冲钻五_牛客小白月赛38 (nowcoder.com)
思路:
没什么好说的 就是正常思路 写出来即可。
AC code:
#include<string>
#include <iostream>
using namespace std;
int ans;
int cmobo;
int k,n,t;
string a;
int main()
{
cin >> t;
while(t --)
{
ans = 0;
cmobo = 0;
cin >> n >> k;
cin >> a;
for(auto& t : a)
{
if(t == 'W')
{
cmobo++;
if(cmobo >= 3)
ans += k;
else
ans++;
}
else
{
cmobo = 0;
ans--;
}
}
cout << ans << endl;
}
return 0;
}
NC41 最长无重复子数组
题目链接:最长无重复子数组_牛客题霸_牛客网 (nowcoder.com)
思路:
用map来实现 类似滑动窗口。注意相同元素多次插入时
AC code:
class Solution {
public:
int maxLength(vector<int>& arr)
{
unordered_map<int, int> mp;
int res = 0;
for(int left = 0, right = 0; right < arr.size(); right++)
{
mp[arr[right]]++;
while(mp[arr[right]] > 1)
mp[arr[left++]]--;
res = max(res, right - left + 1);
}
return res;
}
};
NC379 重排字符串
题目链接:重排字符串_牛客题霸_牛客网 (nowcoder.com)
思路:
这里没到原题 但是找到了封装接口的,思路都一样。
创建大根堆,优先加上出现次数多的字母即可。
AC code:
#include <iostream>
#include<queue>
#include<string>
using namespace std;
const int N = 200;
typedef pair<int, char> PIC;
priority_queue<PIC, vector<PIC>> q;
priority_queue<PIC, vector<PIC>> p;
string s;
int n;
int a[N];
string ans = "5";
int main()
{
cin >> n >> s;
for (auto& t : s)
a[t]++;
for (int i = 0; i < 199; i++)
if (a[i] != 0)
q.push({ a[i],i });
if (q.size() == 1)
{
cout << "no";
return 0;
}
while (q.size())
{
if (q.size() == 1 && q.top().first > 1)
{
cout << "no";
return 0;
}
while (ans.back() == q.top().second)
{
auto t = q.top();
q.pop();
p.push(t);
}
auto t = q.top();
q.pop();
ans += t.second;
t.first--;
if (t.first)
q.push(t);
while (p.size())
{
auto t = p.top();
p.pop();
q.push(t);
}
}
cout << "yes" << endl;
for (int i = 1; i < ans.size(); i++)
cout << ans[i];
return 0;
}