您的位置:首页 > 大数据 > 人工智能

HUAS Summer Trainning #3 E

2015-08-02 18:42 281 查看
Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are calledequivalent 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.

Sample Input

Input
aaba abaa


Output
YES


Input
aabb abab


Output
NO输出

题目大意:给你2个长度都为n的字符串,让你比较这2个字符串是否相同,(要分成相同长度的字符串,奇数直接比较)
例如第一个字符串可以分为A1,A2。第二个可以分为B1,B2。
当A1=B1且A2=B2,或者A1=B2且A2=B1时,输出YES,否则NO。
解题思路:如果字符串的长度为偶数,对半分直到字符串长度分为奇数,每次看是否满足条件,奇数直接进行比较。
(这里就需要用到递归。)
代码:


#include<iostream>
#include<string>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=200000+100;
char d[maxn],b[maxn];
bool dfs(char* a,char* b,int n)
{
if(!strncmp(a,b,n))
return  true;
if(n%2)
return false;
n=n/2;
if(dfs(a,b+n,n)&&dfs(a+n,b,n))
return true;
if(dfs(a,b,n)&&dfs(a+n,b+n,n))
return true;
return false;
}
int main()
{
cin>>d;
cin>>b;
if(dfs(d,b,strlen(d)))
cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}





                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: