【24.34%】【codeforces 560D】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
Note
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”.
【题目链接】:http://codeforces.com/contest/560/problem/D
【题解】
写个递归就过了;
中间如果遇到字符串的长度为奇数则直接返回false;
感觉这样能剪掉挺多情况的。就交了一发;结果真的过了;
事后看到好多人都在第91个点T了。我看了一下500ms左右。不知道为什么;
还是应该对自己自信点吧!
【完整代码】
#include <bits/stdc++.h>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define LL long long
#define rep1(i,a,b) for (int i = a;i <= b;i++)
#define rep2(i,a,b) for (int i = a;i >= b;i--)
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define rei(x) scanf("%d",&x)
#define rel(x) scanf("%I64d",&x)
typedef pair<int,int> pii;
typedef pair<LL,LL> pll;
//const int MAXN = x;
const int dx[9] = {0,1,-1,0,0,-1,-1,1,1};
const int dy[9] = {0,0,0,-1,1,-1,1,-1,1};
const double pi = acos(-1.0);
string s1,s2;
bool f(string a,string b)
{
if (a==b) return true;
int len1 = a.size(),len2 = b.size();
if (len1&1 || len2 &1) return false;
if (len1==1)
return false;
string ta1 = a.substr(0,len1/2),ta2 = a.substr(len1/2,len1/2);
string tb1 = b.substr(0,len2/2),tb2 = b.substr(len2/2,len2/2);
return (f(ta1,tb2)&&f(ta2,tb1))||(f(ta1,tb1)&&f(ta2,tb2));
}
int main()
{
//freopen("F:\\rush.txt","r",stdin);
cin >> s1;cin >> s2;
if (f(s1,s2))
puts("YES");
else
puts("NO");
return 0;
}