2018湘潭邀请赛G题题解

2018湘潭邀请赛G题题解

G. 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 Ar = 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, . . . , tma, bc

• The sum of and does not exceed 250000.

 

Sample Input

ab ba ac ca a ab

 

Sample Output

Yes No No

 

Note

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

 

 

 

题意:

 

读完题目,简化后就是c的位置不能变化,c之间的a和b的位置可以变化。a和b位置可以交换的原因就是有abab的存在。

 

再简化就是统计c之间(或者c至开头或者c至结尾)a和b的数目。详见代码吧。

 

 

AC代码:

 

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;

const int SIZE = 1e4+5;
int main() {
    char arr1[SIZE], arr2[SIZE];
    while(scanf("%s", arr1) != EOF) {
        scanf("%s", arr2);
        int len1 = strlen(arr1), len2 = strlen(arr2);
        int a1a[SIZE] = {0};
        int a1b[SIZE] = {0};
        int a2a[SIZE] = {0};
        int a2b[SIZE] = {0};
        int x = 0, y = 0;
        for(int i = 0; i < len1; i++) {
            if(arr1[i] != 'c') {
                if(arr1[i] == 'a') a1a[x]++;
                else if(arr1[i] == 'b') a1b[x]++;
            } else {
                x++;
            }
        }
        for(int i = 0; i < len2; i++) {
            if(arr2[i] != 'c') {
                if(arr2[i] == 'a') a2a[y]++;
                else if(arr2[i] == 'b') a2b[y]++;
            } else {
                y++;
            }
        }
        bool flag = true;
        if(x != y) printf("No\n");
        else {
            for(int i = 0; i <= x; i++) {
                if(a1a[i] % 2 != a2a[i] % 2 || a1b[i] % 2 != a2b[i] % 2) {
                    printf("No\n");
                    flag = false;
                    break;
                }
            }
            if(flag) printf("Yes\n");
        }
    }
    return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值