【CodeForce】559B Equivalent Strings 等效字符串
【CodeForce】559B Equivalent Strings 等效字符串
B. Equivalent Strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are called equivalent in one of the two cases:
They are equal.
If we split string a into two halves of the same size a1 and a2, and string b into two halves of the same size b1 and b2, then one of the following is correct:
a1 is equivalent to b1, and a2 is equivalent to b2
a1 is equivalent to b2, and a2 is equivalent to b1
As a home task, the teacher gave two strings to his students and asked to determine if they are equivalent.
Gerald has already completed this home task. Now it’s your turn!
Input
The first two lines of the input contain two strings given by the teacher. Each of them has the length from 1 to 200?000 and consists of lowercase English letters. The strings have the same length.
Output
Print “YES” (without the quotes), if these two strings are equivalent, and “NO” (without the quotes) otherwise.
Examples
input
aaba
abaa
output
YES
input
aabb
abab
output
NO
Hint
In the first sample you should split the first string into strings “aa” and “ba”, the second one — into strings “ab” and “aa”. “aa” is equivalent to “aa”; “ab” is equivalent to “ba” as “ab” = “a” + “b”, “ba” = “b” + “a”.
In the second sample the first string can be splitted into strings “aa” and “bb”, that are equivalent only to themselves. That’s why string “aabb” is equivalent only to itself and to string “bbaa”.
翻译
B.等效字符串
每次测试的时限2秒
每次测试的内存限制256兆字节
输入标准输入
输出标准输出
今天,杰拉尔德(Gerald)在有关字符串的讲座中学习了字符串等效的新定义。在以下两种情况之一中,长度相等的两个字符串a和b被称为等效字符串:
他们是平等的。
如果将字符串a分成大小相同的a 1和a 2的两半,并且将字符串b分成大小相同的b 1和b 2的两半,则以下之一是正确的:
a 1等效于b 1, a 2等效于b 2
a 1等效于b 2, a 2等效于b 1
作为一项家庭任务,老师给了他的学生两根弦,并要求他们确定它们是否相等。
杰拉尔德已经完成了这项家庭任务。现在轮到你了!
输入
输入的前两行包含教师给定的两个字符串。每个字母的长度在1到200 000之间,由小写英文字母组成。字符串长度相同。
输出
如果这两个字符串相等,则打印“ YES ”(不带引号),否则打印“ NO ”(不带引号)。
例子
输入复制
aaba
abaa
输出复制
YES
输入复制
aabb
abab
输出复制
NO
解释
在第一个示例中,您应该将第一个字符串拆分为字符串“ aa ”和“ ba ”,将第二个字符串拆分为字符串“ ab ”和“ aa ”。“ aa ”等同于“ aa ”;“ ab ”等同于“ ba ”,因为“ ab ” =“ a ” +“ b ”,“ ba ” =“ b ” +“ a ”。
在第二个示例中,第一个字符串可以分为字符串“ aa ”和“ bb ”,它们仅等效于它们自己。这就是为什么字符串“ aabb ”仅等效于自身和字符串“ bbaa ”的原因。
思路
等效有传递性
求出S,T等效字符串中字典序最小的字符串。
若相同,S等效T
若不相同,S不等效T
代码
#include<iostream>
#include<cstdio>
using namespace std;
string a,b;
string stra(int l,int r)
{
if((r-l+1)&1)return a.substr(l,r-l+1);
string t1=stra(l,(l+r)>>1),t2=stra(((l+r)>>1)+1,r);
if(t1<t2)return t1+t2;
else return t2+t1;
}
string strb(int l,int r)
{
if((r-l+1)&1)return b.substr(l,r-l+1);
string t1=strb(l,(l+r)>>1),t2=strb(((l+r)>>1)+1,r);
if(t1<t2)return t1+t2;
else return t2+t1;
}
int main()
{
cin>>a>>b;
if(stra(0,a.size()-1)==strb(0,b.size()-1))printf("YES");
else printf("NO");
return 0;
}
推荐阅读
-
codeforces 559B Equivalent Strings (最小表示法)
-
【CodeForce】559B Equivalent Strings 等效字符串
-
893. Groups of Special-Equivalent Strings(特殊等价字符串组)
-
CodeForces - 559B Equivalent Strings 分治,递归
-
codeforces 559B Equivalent Strings 分治归并
-
codeforces 559B Equivalent Strings
-
CF 559B(Equivalent Strings-构造法)
-
[codeforces 559B]Equivalent Strings
-
Codeforces 559B Equivalent Strings(分治)
-
Codeforces 559B Equivalent Strings 等价串