HDU - 6282 String Transformation(模拟)

题意:给出两个字符串S和T,只由a,b,c三种字符组成(不为空串,长度不一定相同,不一定包含所有字符)。对字符串S可以进行几种操作:在任意位置添加/删除字符串aa,bb,abab。问字符串S是否能通过以上几种操作变成T,输出Yes/No。

根据题目条件可以得出 

1: aa,bb,abab可以直接删除;

2: ab可以变成ba

3: aba可以变成b,bab可以变成a

由于我比较菜模拟写的很繁琐,贴个代码时刻提醒自己。最后输出的Yes&No 一开始写成的全大写。。。

#include <bits/stdc++.h>

using namespace std;
const int maxn=1e4+100;
char s1[maxn],s2[maxn],a[maxn],b[maxn];
int n,m;
int check2(char s[],int &top)
{
    if(s[top-1]=='a'&&s[top]=='a')
    {
        top-=2;
        return 1;
    }
    if(s[top-1]=='b'&&s[top]=='b')
    {
        top-=2;
        return 1;
    }
    if(s[top-1]=='b'&&s[top]=='a')
    {
        s[top-1]='a';
        s[top]='b';
        return 1;
    }
    return 0;
}
int check3(char s[],int &top)
{
    if(s[top-2]=='a'&&s[top-1]=='b'&&s[top]=='a')
    {
        top-=2;
        s[top]='b';
        return 1;
    }
    if(s[top-2]=='b'&&s[top-1]=='a'&&s[top]=='b')
    {
        top-=2;
        s[top]='a';
        return 1;
    }
    return 0;
}
int check4(char s[],int &top)
{
    if(s[top-3]=='a'&&s[top-2]=='b'&&s[top-1]=='a'&&s[top]=='b')
    {
        top-=4;
        return 1;
    }
    return 0;
}
int main()
{
    ///cout << "Hello world!" << endl;
    //freopen("in.txt","r",stdin);
    while(~scanf("%s%s",a,b))
    {
        n=strlen(a);
        m=strlen(b);
        int top1=0,top2=0;
        for(int i=0;i<n;i++)
        {
            s1[++top1]=a[i];
            while(1)
            {
                int fuck=0;
                if(top1>=4)
                    fuck+=check4(s1,top1);
                if(top1>=3)
                    fuck+=check3(s1,top1);
                if(top1>=2)
                    fuck+=check2(s1,top1);
                if(fuck==0)
                    break;
            }
        }
        for(int i=0;i<m;i++)
        {
            s2[++top2]=b[i];
            while(1)
            {
                int fuck=0;
                if(top2>=4)
                    fuck+=check4(s2,top2);
                if(top2>=3)
                    fuck+=check3(s2,top2);
                if(top2>=2)
                    fuck+=check2(s2,top2);
                if(fuck==0)
                    break;
            }
        }
        int flag=1;
        if(top1!=top2) flag=0;
        for(int i=1;i<=min(top1,top2);i++)
        if(s1[i]!=s2[i])
        {
            flag=0;
            break;
        }
        if(flag) puts("Yes");
        else puts("No");
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值