第一题 A. Is It a Cat?
#include <iostream>
#include <cstdio>
#include <cctype>
using namespace std;
string str = "meow";
void solve()
{
int n;cin >> n;
string s; cin >> s;
for(int i = 0;i < n;i++) s[i] = tolower(s[i]);
// for(int i = 0;i < n;i++) cout << s[i];
// cout << endl;
int j = 0;
if(s[0] != 'm'){
cout << "NO" << endl;
return ;
}
for(int i = 0;i < n ;i++)
{
if(s[i + 1] != s[i])
{
j++;
if(s[i + 1] != str[j])
{
cout << "NO" << endl;
return ;
}
}
}
if(j == 4)
cout << "YES" << endl;
else cout << "NO" << endl;
}
int main()
{
int t;cin >> t;
while(t--)
{
solve();
}
}
其中函数 tolower 是将大写字母转化为小写字母 ;函数 toupper 则是将小写字母转化为大写字母
存在于 <cctype> 头文件中
#include <iostream>
#include <cstdio>
#include <cctype>
#include <algorithm>
using namespace std;
string str = "meow";
void solve()
{
int n;cin >> n;
string s; cin >> s;
for(int i = 0;i < n;i++) s[i] = tolower(s[i]);
s.erase(unique(s.begin(),s.end()),s.end());
if(s == "meow") cout << "YES" << endl;
else cout << "NO" << endl;
return ;
}
int main()
{
int t;cin >> t;
while(t--)
{
solve();
}
}
用到了 unique 去重 !!!!!
第二题
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 150;
int cnt[N];
void solve()
{
for(int i = 0;i < 150;i++) cnt[i] = 0;
int n,k;cin >> n >> k;
string s;cin >> s;
for(int i = 0;i < n;i++)
{
int nu = s[i];
cnt[nu]++;
}
int res = 0;
for(int i = 'A';i <= 'Z'; i++){
res += min(cnt[i],cnt[i+32]);
if(k != 0){
int maxt = max(cnt[i],cnt[i+32]);
int mint = min(cnt[i],cnt[i+32]);
res += min(k,(maxt-mint)/2);
k -= min(k,(maxt-mint)/2);
}
}
cout << res << endl;
}
int main()
{
int t; cin >> t;
while(t--) solve();
return 0;
}
A —— 64 ;a ——97
第三题 C1 第四题C2 用到了优先队列 大根堆排序
#include <iostream>
#include <cstdio>
#include <queue>
using namespace std;
const int N = 1e5 +10;
int a[N];
typedef long long ll;
void solve()
{
int n;cin >> n;
priority_queue<int> q;
queue<int> num;
for(int i = 1;i <= n;i++)
{
scanf("%d",&a[i]);
if(a[i] == 0)
{
if(!q.empty())
{
num.push(q.top());
q.pop();
}
}
else q.push(a[i]);
}
ll res = 0;
while(num.size())
{
res += num.front();
num.pop();
}
cout << res << endl;
}
int main()
{
int t; cin >> t;
while(t--) solve();
return 0;
}