String Transformation(思维题)

十日寒。

7040: String Transformation

时间限制: 1 Sec   内存限制: 128 MB
提交: 69   解决: 21
[ 提交][ 状态][ 讨论版][命题人: admin]

题目描述

Bobo has a string S = s1 s2...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 0 = u ◦ v and vice versa where u , v are (possibly empty) strings and w ∈ { aa , bb , abab } .
Given the target string T = t1 t2 . . . tm , determine if Bobo can transform the string S into T .

输入

The input consists of several test cases and is terminated by end-of-file.
The first line of each test case contains a string s1 s2 ...sn . The second line contains a string t1 t2 . . . tm .

输出

For each test case, print Yes if Bobo can. Print No otherwise.
• 1 ≤ n, m ≤ 104
• s1 , s2 ,..., sn , t1 , t2 , . . . , tm ∈ { a , b , c }
• The sum of n and m does not exceed 250,000.

样例输入

ab

ba

ac

ca

a

ab

样例输出

Yes

No

No

提示

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

来源

2018湘潭邀请赛 


题目链接:http://exam.upc.edu.cn/problem.php?id=7040

题意:给你两个s1,s2,然后可以在任意位置加或减{aa,bb,abab};

问你可以通过变化来s1 得到s2.

后来你会发现这个题其实与a和b的奇偶性有关,(不难想到)。。。

1、两个字符串都没有c的情况下,考虑a和b的奇偶性,只要两个串的a和b的奇偶性相同即可。

2、()C()C()C……其实有C就是把它划分成一个一个区间对区间里的a和b的奇偶性来讨论。

然后就可以直接用一个Vector来实现这个过程了(感谢何仔)

扫描二维码关注公众号,回复: 2321653 查看本文章
#include<stdio.h>
#include<algorithm>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define FAST_IO ios::sync_with_stdio(false)
#define mem(a,b) memset(a,b,sizeof(a))
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const long long INF=0x7FFFFFFFFFFFFFFFLL;
const int inf=0x3f3f3f3f;
const int mod=1e9+7;
typedef long long ll;
using namespace std;

vector<int>f(string s)
{
    vector<int>v;
    int sum=0;
    for(int i=0;i<=s.size();i++)
        if(i==s.size() || s[i]=='c')
        {
            v.push_back(sum);
            sum=0;
        }
        else
            sum^=s[i];

    return v;
}

int main()
{
    string s1,s2;
    while(cin>>s1>>s2)
    {
        if(f(s1)==f(s2))
            printf("Yes\n");
        else
            printf("No\n");
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/sudu6666/article/details/80423703