A Is It a Cat?
解题思路
模拟即可
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n;
string s;
void solve() {
string ss = "meow";
cin >> n >> s;
if (n < 4) {
cout << "NO" << endl;
return;
}
for (int i = 0; i < n; i++) {
if (s[i] <= 'Z' && s[i] >= 'A') {
s[i] += ('a' - 'A');
}
}
char x = s[0];
int t = 0;
if (x != 'm') {
cout << "NO" << endl;
return;
}
for (int i = 1; i < n; i++) {
if (s[i] != x) {
x = s[i];
t++;
if (x != ss[t] || t > 3) {
cout << "NO" << endl;
return;
}
}
}
if (t == 3) cout << "YES" << endl;
else cout << "NO" << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}
B Count the Number of Pairs
解题思路
贪心:统计每一种字母的大小写的数量,如果两者差值大于1,说明还可以消耗次数
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n, k;
string s;
unordered_map<char, int>mp;
void solve() {
mp.clear();
cin >> n >> k >> s;
for (int i = 0; i < n; i++) mp[s[i]]++;
ll cnt = 0;
for (int i = 'a'; i <= 'z'; i++) {
int j = i - ('a' - 'A');
if (mp[i] < mp[j]) swap(mp[i], mp[j]);
cnt += min(mp[i], mp[j]);
while (k && mp[i] - mp[j] > 1) {
k--;
mp[i]--, mp[j]++;
cnt++;
}
}
cout << cnt << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}
C Powering the Hero (hard version)
解题思路
贪心:每次遇到0,就取前个种最大的,可以用优先队列实现
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n, x;
priority_queue<ll>q;
void solve() {
while (!q.empty()) q.pop();
cin >> n;
ll ans = 0;
for (int i = 1; i <= n; i++) {
cin >> x;
if (x) q.push(x);
else {
if (!q.empty()) {
ans += q.top();
q.pop();
}
}
}
cout << ans << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}
D Remove Two Letters
解题思路
如果,那么答案计数减1
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n;
string s;
ll cnt;
void solve() {
cin >> n >> s;
cnt = n;
for (int i = 1; i < n - 1; i++) {
if (s[i - 1] == s[i + 1]) cnt--;
}
cout << cnt - 1 << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}
E Unforgivable Curse (hard version)
解题思路
找规律,发现只有特定的位置可以互换,对于不能互换的位置,如果不相等,那么就是NO了
参考代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int N = 2e5 + 5;
ll T;
ll n, k;
string a, b;
int dk1[4] = {0, 0, 1, 1};
int dk2[4] = {1, -1, 1, -1};
unordered_map<char, int>mpa, mpb;
void solve() {
mpa.clear(), mpb.clear();
cin >> n >> k >> a >> b;
if (a == b) {
cout << "YES" << endl;
return;
}
for (int i = 0; i < n; i++) mpa[a[i]]++, mpb[b[i]]++;
if (n <= k || mpa != mpb) {
cout << "NO" << endl;
return;
}
if (n >= 2 * k) {
cout << "YES" << endl;
return;
}
int x = n - k;
for (int i = x; i <= n - x - 1; i++) {
if (a[i] != b[i]) {
cout << "NO" << endl;
return;
}
}
cout << "YES" << endl;
}
int main() {
cin >> T;
while (T--) solve();
return 0;
}