列表

详情


NC52897. String Transformation

描述

Bobo has a string consists of letter `a`, `b` and `c`.
He can transform the string by inserting or deleting substrings `aa`, `bb` and `abab`.

Formally, (``'' denotes string concatenation) can be transformed into and vice versa where u, v are (possibly empty) strings and .

Given the target string , 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 .
The second line contains a string .

输出描述

For each test case, print `Yes` if Bobo can. Print `No` otherwise.

示例1

输入:

ab
ba
ac
ca
a
ab

输出:

Yes
No
No

说明:

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

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++11(clang++ 3.9) 解法, 执行用时: 38ms, 内存消耗: 480K, 提交时间: 2019-10-04 14:38:59

#include <bits/stdc++.h>
 
using namespace std;
const int N = 1e4+1;
char s[N],b[N];
 
string solve(char *s)
{
    string b = "";
    int sum = 0;
    for(int i = 0; s[i]; ++i){
        if(s[i]=='c')
            b+=sum, sum = 0;
        else
            sum ^= s[i];
    }
    return b+=sum;
}
 
int main()
{
    while(cin>>s>>b)
    {
        puts((solve(s).compare(solve(b))==0)?"Yes":"No");
    }
    return 0;
}

C++14(g++5.4) 解法, 执行用时: 43ms, 内存消耗: 340K, 提交时间: 2019-10-02 18:39:17

#include <bits/stdc++.h>

using namespace std;
const int N = 1e4+1;
char s[N],b[N];

string solve(char *s)
{
    string b = "";
    int sum = 0;
    for(int i = 0; s[i]; ++i){
        if(s[i]=='c')
            b+=sum, sum = 0;
        else
            sum ^= s[i];
    }
    return b+=sum;
}

int main()
{
    while(cin>>s>>b)
    {
        puts((solve(s).compare(solve(b))==0)?"Yes":"No");
    }
    return 0;
}

Python3(3.5.2) 解法, 执行用时: 71ms, 内存消耗: 4032K, 提交时间: 2019-10-02 14:51:00

import sys

for src, dst in zip(*[iter(sys.stdin)]*2):
    src = src.strip()
    dst = dst.strip()
    srcl = src.split('c')
    dstl = dst.split('c')
    if len(srcl) != len(dstl):
        print("No")
        continue
    for s, d in zip(srcl, dstl):
        if s.count('a')%2 != d.count('a')%2 or s.count('b')%2 != d.count('b')%2:
            print("No")
            break
    else:
        print("Yes")

上一题