2019牛客国庆集训派对day2,G(思维)

首先,这道题要明确两点:
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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值