String Transformation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 57 Accepted Submission(s): 17
Problem Description
Bobo has a string
S=s1s2…sn consists of letter `a`, `b` and `c`.
He can transform the string by inserting or deleting substrings `aa`, `bb` and `abab`.
Formally, A=u∘w∘v (`` ∘'' denotes string concatenation) can be transformed into A′=u∘v and vice versa where u, v are (possibly empty) strings and w∈{aa,bb,abab}.
Given the target string T=t1t2…tm, determine if Bobo can transform the string S into T.
He can transform the string by inserting or deleting substrings `aa`, `bb` and `abab`.
Formally, A=u∘w∘v (`` ∘'' denotes string concatenation) can be transformed into A′=u∘v and vice versa where u, v are (possibly empty) strings and w∈{aa,bb,abab}.
Given the target string T=t1t2…tm, determine if Bobo can transform the string S into T.
Input
The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains a string s1s2…sn.
The second line contains a string t1t2…tm.
The first line of each test case contains a string s1s2…sn.
The second line contains a string t1t2…tm.
Output
For each test case, print `Yes` if Bobo can. Print `No` otherwise.
## Constraint
* 1≤n,m≤104
* s1,s2,…,sn,t1,t2,…,tm∈{a,b,c}
* The sum of n and m does not exceed 250,000.
## Constraint
* 1≤n,m≤104
* s1,s2,…,sn,t1,t2,…,tm∈{a,b,c}
* The sum of n and m does not exceed 250,000.
Sample Input
abbaaccaaab
Sample Output
YesNoNo
Hint
For the first sample, Bobo can transform as `ab => aababb => babb => ba`.
//deque:双端队列,遇到aa,bb就删除,以c作为分隔符,两个c不一样多就输出no
后面应当每个c前后只剩下ab||ba||b||a
空则push,不然进行判断,相等pop且不放,不同,判断之后的,否则放
#include <iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<deque>
#define ll long long
using namespace std;
char a[10005];
char b[10005];
deque<int>q1,q2;
int main()
{
while(cin>>a>>b)
{
while(!q1.empty ()) q1.pop_back();
while(!q2.empty ()) q2.pop_back();
int la=strlen(a);
int lb=strlen(b);
/* if((la%2)!=(lb%2))
{
printf("No\n");
continue;
}*/
int c1=0;
int c2=0;
for(int i=0;i<la;i++)
{
if(a[i]=='c')
{
q1.push_back(3);
c1++;
}
else if(a[i]=='a')
{
if(q1.empty ())
{
q1.push_back(1);
continue;
}
if(q1.back()==1)
{
q1.pop_back();
}
else if(q1.back()==2)
{
if(i<la-1&&a[i+1]=='b')
{
q1.pop_back();
i++;
if(q1.empty ()||q1.back ()!=1)
{
q1.push_back (1);
}
else if(q1.back ()==1)
{
q1.pop_back();
}
}
else q1.push_back (1);
}
else q1.push_back (1);
}
else if(a[i]=='b')
{
if(q1.empty ())
{
q1.push_back(2);
continue;
}
if(q1.back()==2)
{
q1.pop_back();
}
else if(q1.back()==1)
{
if(i<la-1&&a[i+1]=='a')
{
q1.pop_back();
i++;
if(q1.empty ()||q1.back ()!=2)
{
q1.push_back (2);
}
else if(q1.back ()==2)
{
q1.pop_back();
}
}
else q1.push_back(2);
}
else q1.push_back(2);
}
}
for(int i=0;i<lb;i++)
{
if(b[i]=='c')
{
q2.push_back(3);
c2++;
}
else if(b[i]=='a')
{
if(q2.empty ())
{
q2.push_back(1);
continue;
}
if(q2.back()==1)
{
q2.pop_back();
}
else if(q2.back()==2)
{
if(i<lb-1&&b[i+1]=='b')
{
q2.pop_back();
i++;
if(q2.empty ()||q2.back ()!=1)
{
q2.push_back (1);
}
else if(q2.back ()==1)
{
q2.pop_back();
}
}
else q2.push_back (1);
}
else q2.push_back (1);
}
else if(b[i]=='b')
{
if(q2.empty ())
{
q2.push_back(2);
continue;
}
if(q2.back()==2)
{
q2.pop_back();
}
else if(q2.back()==1)
{
if(i<lb-1&&b[i+1]=='a')
{
q2.pop_back();
i++;
if(q2.empty ()||q2.back ()!=2)
{
q2.push_back (2);
}
else if(q2.back ()==2)
{
q2.pop_back();
}
}
else q2.push_back(2);
}
else q2.push_back(2);
}
}
bool f=1;
if(c1!=c2||q1.size()!=q2.size()) f=0;
else
{
while(!q1.empty ())
{
if(q1.front() ==1&&q2.front() ==2)
{
q1.pop_front ();
q2.pop_front ();
if(q1.empty ())
{
f=0;
break;
}
else if(q1.front() ==2&&q2.front() ==1)
{
q1.pop_front ();
q2.pop_front ();
}
else
{
f=0;
break;
}
}
else if(q1.front() ==2&&q2.front() ==1)
{
q1.pop_front ();
q2.pop_front ();
if(q1.empty ())
{
f=0;
break;
}
else if(q1.front() ==1&&q2.front() ==2)
{
q1.pop_front ();
q2.pop_front ();
}
else
{
f=0;
break;
}
}
else if(q1.front()==q2.front ())
{
q1.pop_front();
q2.pop_front();
}
else
{
f=0;
break;
}
}
}
if(f) printf("Yes\n");
else printf("No\n");
}
return 0;
}
[ Copy to Clipboard ] [ Save to File]