HDU 6282 String Transformation

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 .

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

For the first sample, Bobo can transform as ab => aababb => babb => ba.

題解:由ab->aababb->ba,可推得abab…ab-> baba…ba(此處a與b數量相等)。若出現aa或bb時可以消去aa或bb,說明沒有c時只要a,b奇偶相同就一定可以推出答案。有c時就將c當做分隔符,將數組分成幾段,判斷這些小段中a,b奇偶是否相同即可。
判斷a,b奇偶是否相同用異或,相同時a^a^…^a=0,b^b^…^b=0,a^b^a^b^…^a^b=0,
不同時不爲零。

代碼如下:

#include<stdio.h>
#include<string>
#include<vector>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;

vector<int> transform(string s)
{
    vector<int> v;
    int len=s.size();
    int sum=0;
    for(int i=0;i<=len;i++)
    {
        if(i==len||s[i]=='c')
        {
            v.push_back(sum);//用vector保存每一小段的sum值
            sum=0;//更新sum爲0
        }
        else
            sum^=s[i];//異或
    }
    return v;
}
int main()
{
    string s,t;
    while(cin>>s>>t)
    {
        if(transform(s)==transform(t))
            cout<<"Yes"<<endl;
        else
            cout<<"No"<<endl;
    }

return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章