目录
037:牛牛冲钻五
题解:简单模拟
#include <iostream>
#include <string>
using namespace std;
int t, n, k;
string s;
int fun()
{
int ret = 0;
for(int i = 0; i < s.size(); i++)
{
if(s[i] == 'L')
{
ret--;
}
else
{
if(i - 1 >= 0 && i - 2 >= 0 && s[i - 1] == 'W' && s[i - 2] == 'W')
{
ret += k;
}
else
{
ret += 1;
}
}
}
return ret;
}
int main()
{
cin >> t;
while(t--)
{
cin >> n >> k >> s;
cout << fun() << endl;
}
return 0;
}
038:最长无重复子数组
最长无重复子数组_牛客题霸_牛客网 (nowcoder.com)
题解:经典滑动窗口
class Solution {
int hash[100010]={0};
public:
int maxLength(vector<int>& arr)
{
//滑动窗口
int n=arr.size();
int right=0,left=0,ret=0;
while(right<n)
{
hash[arr[right]]++;//进窗口
while(hash[arr[right]]>1)//判断
{
hash[arr[left]]--;//出窗口
left++;
}
ret=max(ret,right-left+1);
right++;
}
return ret;
}
};
039:重排字符串
题解:贪心
#include<iostream>
using namespace std;
const int N=1e5+10;
int cnt[26]={0};
char s[N];
char ret[N];
int n;
int main()
{
cin>>n>>s;
char maxchar;
int maxcount=0;
for(int i=0;i<n;i++)
{
cnt[s[i]-'a']++;
if(cnt[s[i]-'a']>maxcount)
{
maxcount=cnt[s[i]-'a'];
maxchar=s[i];
}
}
cout<<maxcount<<" "<<maxchar<<endl;
if(maxcount>(n+1)/2) cout<<"No"<<endl;
else
{
cout<<"Yes"<<endl;
int index=0;
//先填最多数量的字符
while(maxcount--)
{
ret[index]=maxchar;
index+=2;
}
//填剩下的
for(int i=0;i<26;i++)
{
if(cnt[i] && i+'a'!=maxchar)
{
while(cnt[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;
}