A. Is It a Cat ?
原题链接:Is It a Cat?
思路:直接模拟。
#include<bits/stdc++.h>
#include <Windows.h>
#define ll long long
using namespace std;
//ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
const ll N=1e6+5;
const ll M=1e9;
int main()
{
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
int _;cin>>_;
while(_--)
{
int n;string mo;cin>>n>>mo;
char p;int s=0;
for(int i=0;i<n;i++)
{
if(i==0)
{
if((mo[i]!='m'&&mo[i]!='M'))
{
s=1;
//cout<<2<<"?";
break ;
}
else
{
p='m';
}
}
if(p==mo[i]||p==(mo[i]+32)) { continue ; }
if(p=='m')
{
if(mo[i]=='e'||mo[i]=='E')
{
p='e';
}
else
{
s=1;
break ;
}
}
else if(p=='e')
{
if(mo[i]=='o'||mo[i]=='O')
{
p='o';
}
else
{
s=1;
break ;
}
}
else if(p=='o')
{
if(mo[i]=='w'||mo[i]=='W')
{
p='w';
}
else
{
s=1;
break ;
}
}
else if(p=='w')
{
s=1;
break ;
}
}
if(s==0&&p=='w')
{
cout<<"YES\n";
}
else
{
cout<<"NO\n";
}
}
return 0;
}
B. Count the Number of Pairs
原题链接:Count the Number of Pairs
题意:给定一个字符串,同一字母的大写和小写对应算作一对,提供一个value,可以进行最多k次变换(可以小于等于k次),每次变化可以选择将小写字母改成大写字母或将大写字母改成小写,求最多k次后value最大值(即对应大小写最多多少对)
思路:先分别统计原字符串的value值(即sum),不经过改变时,对应每个字符大写字母数量s1和小写字母数量s2。如果两者差值=2,即可用一次转换是的改差值变为1value。统计理论上能通过转换获得的额外value和(即ans),如果ans<=k,输出sum+ans;反之输出sum+k。
#include<bits/stdc++.h>
#include <Windows.h>
#define ll long long
using namespace std;
//ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
const ll N=1e6+5;
const ll M=1e9;
map<char,int> mm;
int main()
{
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
int _;cin>>_;
while(_--)
{
int n,k;cin>>n>>k;
string mo;cin>>mo;
int sum=0,ans=0;
mm.clear();
for(int i=0;i<n;i++)
{
mm[mo[i]]++;
}
for(char j='a';j<='z';j++)
{
sum+=min(mm[j],mm[j-32]);
ans+=(abs(mm[j]-mm[j-32])/2);
}
if(ans<=k) cout<<sum+ans<<"\n";
else cout<<sum+k<<"\n";
}
return 0;
}
C1 C2 . Powering the Hero
原题链接: c1: Powering the Hero (easy version)
c2:Powering the Hero (hard version)
这两题除了数据范围不同外思路一样,拿C2的数据来讲
题意:给定一个序列,表示牌堆,包含两种卡:一种为奖励卡(a[i]!=0),一种为英雄卡(a[i]=0)。当已获取的奖励卡不为0时,可以选择将一个英雄卡加入军队,使其数值为任意一已获得的奖励卡值,求军队最大值。
思路:用优先队列承装奖励卡,当队列不为空时获得英雄卡可以获得value并加入军队,否则舍弃。
#include<bits/stdc++.h>
#include <Windows.h>
#define ll long long
using namespace std;
//ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
const ll N=2e5+5;
const ll M=1e9;
ll mo[N];
int main()
{
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
int _;cin>>_;
while(_--)
{
int n;cin>>n;
ll sum=0;
priority_queue <ll,vector<ll>,less<ll> > q;
for(int i=1;i<=n;i++)
{
cin>>mo[i];
if(mo[i]!=0)
{
q.push(mo[i]);
}
else
{
if(q.empty()) continue ;
ll y=q.top();
sum+=y;
q.pop();
}
}
cout<<sum<<"\n";
}
return 0;
}
D. Remove Two Letters
原题链接:Remove Two Letters
题意:给定一个字符串,可以选择删除任意相连的两个字符,问删除后剩下的字符最多有多少种不同情况
思路:如果暴力去想的话,一定是从头枚举每删除两个连续字符剩下的字串的情况,并计数。那么不难想到,当存在i(i>=1&&i<n-1),使得s[i-1]!=s[i+1]时,前一个序列对必将对结果有贡献。注意:此时忽略了后一对对结果有贡献是情况,所以输出sum+1。
#include<bits/stdc++.h>
#include <Windows.h>
#define ll long long
using namespace std;
//ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
const ll N=2e5+5;
const ll M=1e9;
int main()
{
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
int _;cin>>_;
while(_--)
{
int n;cin>>n;
string mo;cin>>mo;
ll sum=0;
for(int i=1;i<n-1;i++)
{
if(mo[i-1]!=mo[i+1])
sum++;
}
cout<<sum+1<<"\n";
}
return 0;
}
E1 E2. Unforgivable Curse
原题链接:E1:Unforgivable Curse (easy version)
E2:Unforgivable Curse (hard version)
同C题一样,两题思路通用,故以E2为例讲解
题意:给定一个k,以及两个字符串s、t。可以对于s进行变换当且仅当|j-i|=k或|j-i|=k+1。可以进行无数次这样的变换,问是否能使得s==t?
思路:当给定的字符串s各个字母数量和t不相等时一定无法进行转换。
之后,进行手动推敲不难发现:
对于任意的 n>=2*k 输出 YES (手写一个串就能发现,此时对于任意一个字符,他都可以移动到该字符串的任意位置)
对于任意的 n>k 从第 n-k 个下标(对于下标范围为0-n-1)开始,之后的 2*k-n 个字符无法通过任何移动到达,因此,如果这个范围内s字符串对应字符和t相同,输出 YES 否则 NO
对于任意的 n<=k 无法进行任何变换,用compare函数判断,相同YES 否则 NO即可
#include<bits/stdc++.h>
#define ll long long
using namespace std;
//ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
const ll N=1e6+5;
const ll M=998244353;
map<char,int> ms,mt;
int main()
{
ios::sync_with_stdio(false) ; cin.tie(0) ; cout.tie(0) ;
int _;cin>>_;
while(_--)
{
int n,k;cin>>n>>k;
string s,t;cin>>s>>t;
int cs=0;
ms.clear();mt.clear();
for(int i=0;i<n;i++)
{
ms[s[i]]++;
mt[t[i]]++;
}
if(ms.size()!=mt.size()) { cout<<"NO\n"; continue ;}
for(auto p:ms)
{
if(p.second!=mt[p.first])
{
cs=1;break ;
}
}
if(cs==1) { cout<<"NO\n"; continue ;}
if(n==1)
{
if(s[0]==t[0]) { cout<<"YES\n"; continue ;}
else { cout<<"NO\n"; continue ;}
}
else if(n>=k*2) { cout<<"YES\n"; continue ;}
else if(n>k)
{
int y=k*2-n;
int i=n-k;
while(y--)
{
if(s[i]!=t[i])
{
cs=1;
break ;
}
i++;
}
if(cs==1) { cout<<"NO\n"; continue ;}
else { cout<<"YES\n"; continue ;}
}
else
{
if(s.compare(t)==0) { cout<<"YES\n"; continue ;}
else { cout<<"NO\n"; continue ;}
}
}
return 0;
}
F. Dasha and Nightmares
原题链接:Dasha and Nightmares