首先,这道题要明确两点:
1.因为可以删增的字符串当中没有一个包含’c’的,因此s,t中的c的个数必须相等才有可能Yes,否则必为No;
2.因为可以删增的字符串当中,a,b的个数都是偶数,因此s,t中a的个数的奇偶性必须相同,以及b的个数奇偶性必须相同,才有可能Yes,否则必为No。
不难分析出,s,t中的c把s,t分成一段段的,在c的个数相等的前提下,我们只要分析s中的每一小段是否能变成t中对应的每一小段就可以。对应于每一小段,它们同样要满足条件2。
在满足c的个数相等和条件2的前提下,我们分别枚举出长度为1,2,3的字符串,发现只要满足条件2,这些字符串之间都可以相互转化,于是我们可以推广到长度为4,5,6…,这些字符串之间都可以相互转化,因为长度>3的字符串可以切分成一小段一小段长度<=3的字符串。
综上,只要c的个数相等,对应于每一小段的字符串满足条件2,就可以输出Yes,否则输出No.
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
const int maxn=1e4+5;
int poss[maxn],post[maxn];
string s,t;
bool check(string s0,string t0)
{
if(s0.length()<t0.length()) swap(s0,t0);
int numa[2]={0},numb[2]={0};
for(int i=0;i<s0.length();++i)
if(s0[i]=='a') numa[0]++;
else numb[0]++;
for(int i=0;i<t0.length();++i)
if(t0[i]=='a') numa[1]++;
else numb[1]++;
if(numa[0]<numa[1]) swap(numa[0],numa[1]);
if(numb[0]<numb[1]) swap(numb[0],numb[1]);
if((numa[0]-numa[1])%2!=0||(numb[0]-numb[1])%2!=0)
return 0;
return 1;
}
int main()
{
ios::sync_with_stdio(false);
while(cin>>s>>t)
{
int cnts=0,cntt=0;
s.push_back('c'); t.push_back('c');
for(int i=0;i<s.length();++i)
if(s[i]=='c')
poss[cnts++]=i;
for(int i=0;i<t.length();++i)
if(t[i]=='c')
post[cntt++]=i;
if(cnts!=cntt)
cout<<"No\n";
else
{
bool flag=1;
for(int i=0;i<cnts;++i)
{
string s0,t0;
for(int j= !i? 0:poss[i-1]+1 ;j<poss[i];++j)
s0.push_back(s[j]);
for(int j= !i? 0:post[i-1]+1 ;j<post[i];++j)
t0.push_back(t[j]);
if(!check(s0,t0))
flag=0;
if(!flag) break;
}
if(flag) cout<<"Yes\n";
else cout<<"No\n";
}
}
return 0;
}