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;
}


 

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