可以把字符串全转换为小写,去重再删除,判断是不是meow
#include <bits/stdc++.h>
using namespace std;
int n,l;
string s;
int main() {
cin>>n;
while (n--) {
cin>>l>>s;
for (int i = 0; i < l; i++) {
s[i] = tolower(s[i]);
}
s.erase(unique(s.begin(),s.end()) , s.end()) ;
printf("%s\n", s=="meow"?"YES":"NO");
}
}
#include<bits/stdc++.h>
using namespace std;
int main()
{
ios :: sync_with_stdio(false);
cin.tie(0),cout.tie(0);
int T;
cin>>T;
while(T--)
{
int cnt = 0;
int n,k;
cin>>n>>k;
string ch;
cin>>ch;
unordered_map<char,int> m;
for(int i = 0 ; i < n; i++)
{
m[ch[i]]++;
}
for(int i = 0 ; i < 26 ; i++)
{
int h = min(m[i+'a'] , m[i+'A']);
cnt += h;
if(k > 0)
{
cnt += min(k,abs(m[i+'a'] - m[i+'A'])/2);
k -= min(k,abs(m[i+'a']-m[i+'A'])/2);
}
}
cout<<cnt<<endl;
}
return 0;
}
C1. Powering the Hero (easy version)
题目意思是每次从堆顶拿出一张卡,如果是0则可以从能量堆中得到相应数值的能量,如果非0则放入能量堆中或discard?求获得的最大能量。可以用优先队列来维护。
#include<iostream>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
int main()
{
ios :: sync_with_stdio(false);
cin.tie(0) , cout.tie(0);
int T;
cin>>T;
while(T--)
{
int n ;
cin>>n;
ll ans = 0;
priority_queue<ll> q;
for(int i = 0 ; i < n ; i++)
{
ll x;
cin>>x;
if(x != 0) q.push(x);
else if(!q.empty())
{
ans += q.top();
q.pop();
}
}
cout<<ans<<endl;
}
return 0;
}
C2. Powering the Hero (hard version)
同上
#include<iostream>
#include<queue>
#include<algorithm>
#define ll long long
using namespace std;
int main()
{
ios :: sync_with_stdio(false);
cin.tie(0) , cout.tie(0);
int T;
cin>>T;
while(T--)
{
int n ;
cin>>n;
ll ans = 0;
priority_queue<ll> q;
for(int i = 0 ; i < n ; i++)
{
ll x;
cin>>x;
if(x != 0) q.push(x);
else if(!q.empty())
{
ans += q.top();
q.pop();
}
}
cout<<ans<<endl;
}
return 0;
}
一开始用哈希表怎么都不行。纯思维,如果一个字符串每个字符都不同的话那么答案就是字符个数-1.如果当前删除的这两个字符的前一个字符和接下来要删除的后一个字符一样的话,那么对答案的贡献度为0。
#include<bits/stdc++.h>
using namespace std;
void solve()
{
int n;
string str;
cin>>n;
cin>>str;
int ans = n - 1;
for(int i = 1 ; i + 1 < n ; i++)
{
if(str[i-1] == str[i+1]) ans--;
}
cout<<ans<<endl;
}
int main()
{
int T;
cin>>T;
while(T--)
{
solve();
}
return 0;
}