Article directory
A. Is It a Cat?
Topic to the effect
There is a string that judges whether meow is satisfied without distinguishing between case and repeated letters.
topic analysis
This question can be judged by the marking method, or can be quickly solved by the C++ function
tolower
Function: You can change uppercase letters to lowercase lettersunique
Function: It can remove repeated letters, and the return value is the address of the last letter after removing the repeated letters, but the length of the string will not be changederase
Function: can clear the characters in the corresponding range
code
#include<bits/stdc++.h>
using namespace std;
int n, m, k, t;
void solve()
{
string s;
cin >> n >> s;
for(int i = 0; i < n; i ++) s[i] = tolower(s[i]);
s.erase(unique(s.begin(), s.end()), s.end()) ;
//cout << s << "--\n";
if(s == "meow") puts("YES");
else puts("NO");
}
int main()
{
cin >> t;
while(t --) solve();
return 0;
}
B. Count the Number of Pairs
Topic to the effect
There is a string composed of uppercase and lowercase letters. Each operation can change the case of a letter. When the number of operations is not more than k, what is the maximum number of pairs of uppercase and lowercase letters that match.
topic analysis
You can first count how many times different characters appear, and then traverse each pair of letters. If there are two A
and four a
, then there are two left after two pairs are formed A
, and one pair can be added by changing one of them to lowercase.
code
#include<bits/stdc++.h>
using namespace std;
int n, m, k, t;
void solve()
{
map<char, int>q;
string s;
cin >> n >> k >> s;
for(int i = 0; i < s.size(); i ++) q[s[i]] ++;
int ans = 0;
for(int i = 0; i < 26; i ++)
{
int r1 = q['a' + i], r2 = q['A' + i];
ans += min(r1, r2);
if(k)
{
int tem = max(r1, r2) - min(r1, r2);
tem /= 2;
if(tem <= k) ans += tem, k -= tem;
else ans += k, k = 0;
}
}
cout << ans << "\n";
}
int main()
{
cin >> t;
while(t --) solve();
return 0;
}
C. Powering the Hero
Topic to the effect
There are n cards in a deck, and each card is characterized by its power. There are two types of cards: a hero card 0; bonus cards, positive integers. You can do the following with your deck: Take a card from the top of the deck; if the card is a bonus card, you can put it on top of your bonus deck or discard it; if the card is a hero card, then The power of the top card in your reward deck is added to his power (if it is not empty), after which the hero is added to your army and the reward used is discarded. Your task is to use these actions to gather an army with the greatest possible total strength.
topic analysis
You can use the priority queue to solve the problem, adding numbers to the queue continuously, and when zero is encountered, the value of the head of the team will be accumulated and the head of the queue will be popped up.
code
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m, k, t;
void solve()
{
priority_queue<int>q;
cin >> n;
int ans = 0;
while(n --)
{
int u; cin >> u;
if(u) q.push(u);
else if(!q.empty())
{
ans += q.top();
q.pop();
}
}
cout << ans << "\n";
}
signed main()
{
cin >> t;
while(t --) solve();
return 0;
}
D. Remove Two Letters
Topic to the effect
There is a string of lowercase letters. Removes two consecutive characters from the string s, how many different strings can be obtained after such an operation.
topic analysis
We can traverse and delete every two adjacent characters. If the letters removed by two adjacent operations are the same, it will not contribute to the answer. Therefore, it is only necessary to judge whether the characters to be clicked by two adjacent operations are the same.
Don't forget to add 1, because each comparison is based on the previous one, so the first one should be counted.
code
#include<bits/stdc++.h>
using namespace std;
int n, m, k, t;
void solve()
{
string s;
cin >> n >> s;
int ans = 0;
for(int i = 1; i < n - 1; i ++)
if(s[i - 1] != s[i + 1]) ans ++;
cout << ans + 1 << "\n";
}
int main()
{
cin >> t;
while(t --) solve();
return 0;
}