当前位置: 代码迷 >> 综合 >> HDU - 6282 String Transformation(模拟)
  详细解决方案

HDU - 6282 String Transformation(模拟)

热度:15   发布时间:2024-01-04 09:53:02.0

本文参考自: 原文地址

题意:给出两个字符串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;
}


  相关解决方案