CCPC2018-湖南全國邀請賽 G String Transformation
阿新 • • 發佈:2018-05-20
iostream ear out delet eve 麻煩 感謝 back 所在
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 .
The first line of each test case contains a string s1s2…sn
.
The second line contains a string t1t2…tm .
## 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 .
For the first sample, Bobo can transform as `ab => aababb => babb => ba`.
String Transformation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 36 Accepted Submission(s):
8
He can transform the string by inserting or deleting substrings `aa`, `bb` and `abab`.
Formally, A=u°w°v
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 .
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 .
Sample Input ab ba ac ca a ab
Sample Output Yes No No Hint
Source CCPC2018-湖南全國邀請賽-重現賽(感謝湘潭大學) 麻煩的模擬題,不管多麻煩的ababa的式子(不含c時)最終只會化成三種形式:a,b,ab(或ba),題目中插入刪除abab的意義所在就是告訴我們ab能夠轉化成ba 我的思路:將兩個式子化解成最優。 eg:abaabcaacabc 化簡 accabc,化簡使用隊列,邊放進去邊判,隊末尾是a,要放的也是a,那就pop(a) ababaccbac 化簡 accbac
然後在從隊首把他們取出來進行比較,特判一下ab和ba是一樣的。
#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); 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; }
CCPC2018-湖南全國邀請賽 G String Transformation