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