String Transformation
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 553 Accepted Submission(s): 175
Problem Description
Bobo has a string S = s_1s_2\dots s_n consists of letter `a`, `b` and `c`.
He can transform the string by inserting or deleting substrings `aa`, `bb` and `abab`.
Formally, A = u \circ w \circ v (``\circ'' denotes string concatenation) can be transformed into A' = u \circ v and vice versa where u, v are (possibly empty) strings and w \in \{\mathrm{aa}, \mathrm{bb}, \mathrm{abab}\}.
Given the target string T = t_1t_2\dots t_m, 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 s_1 s_2\dots s_n.
The second line contains a string t_1t_2\dots t_m.
Output
For each test case, print `Yes` if Bobo can. Print `No` otherwise.
## Constraint
* 1 \leq n, m \leq 10^4
* s_1, s_2, \dots, s_n, t_1, t_2, \dots, t_m \in \{\mathrm{a}, \mathrm{b}, \mathrm{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
题意:给出字符串s和t,可以在s的任意位置插入或删除 'aa,bb,abab',问s能否变成t,注意输入只包含a,b,c
结论:
1 可以构造和完全删除任意连续字串仅当该子串满足a的个数和b的个数都为偶数且没有c
2 任何连续的ab和ba等价。
3 对于连续的a和b,如果个数为偶数,可以无视,否则视为一个a或一个b
将两个字符串重新构造,对于任意连续的无c子串,如果出现奇数个a则添加a,b同理。比较两个新字符串是否相同。
#include<stdio.h>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxm = 100005;
#define ll long long
char a[maxm], b[maxm], aa[maxm], bb[maxm];
int main()
{
int n, i, j, k, sum, now, cnta, cntb;
while (scanf("%s", a) != EOF)
{
sum = 0;
for (i = 0;a[i] != '\0';i++)
if (a[i] == 'c') sum++;
scanf("%s", b);
for (i = 0;b[i] != '\0';i++)
if (b[i] == 'c') sum--;
if (sum != 0)
{
printf("No\n");
continue;
}
cnta = 0, now = 1;
int na = 0, nb = 0;
for (i = 0;a[i] != '\0';i++)
{
if (a[i] == 'c')
{
if (a[i] == a[i - 1])
aa[++cnta] = a[i];
else
{
if (na % 2 == 1) aa[++cnta] = 'a';
if (nb % 2 == 1) aa[++cnta] = 'b';
aa[++cnta] = a[i];
na = nb = 0;
}
}
else if (a[i] == 'a') na++;
else nb++;
}
if (na % 2 == 1) aa[++cnta] = 'a';
if (nb % 2 == 1) aa[++cnta] = 'b';
now = 1, cntb = na = nb = 0;
for (i = 0;b[i] != '\0';i++)
{
if (b[i] == 'c')
{
if (b[i] == b[i - 1])
bb[++cntb] = b[i];
else
{
if (na % 2 == 1) bb[++cntb] = 'a';
if (nb % 2 == 1) bb[++cntb] = 'b';
bb[++cntb] = b[i];
na = nb = 0;
}
}
else if (b[i] == 'a') na++;
else nb++;
}
if (na % 2 == 1) bb[++cntb] = 'a';
if (nb % 2 == 1) bb[++cntb] = 'b';
if (cnta != cntb)
{
printf("No\n");
continue;
}
for (i = 1;i <= cnta;i++)
{
if (aa[i] != bb[i])
break;
}
if (i <= cnta) printf("No\n");
else printf("Yes\n");
}
return 0;
}