Codeforces Round #313 (Div. 1) B.Equivalent Strings

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.

Sample test(s) 
input 
aaba 
abaa 
output 
YES 
input 
aabb 
abab 
output 
NO

题解:爆搜可破

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #include<algorithm>
 5 #include<queue>
 6 #include<cstring>
 7 #define PAU putchar(' ')
 8 #define ENT putchar('
')
 9 using namespace std;
10 const int maxn=200000+10;
11 char s[maxn],t[maxn];
12 bool dfs(int q,int w,int e,int r){
13     int i,j;for(i=q,j=e;i<=w;i++,j++)if(s[i]!=t[j])break;
14     if(i>w)return true;
15     if(((w-q+1)&1)||((r-e+1)&1))return false;int m1=w+q+1>>1,m2=e+r+1>>1;
16     return (dfs(q,m1-1,m2,r)&&dfs(m1,w,e,m2-1))||(dfs(q,m1-1,e,m2-1)&&dfs(m1,w,m2,r));
17 }
18 inline int read(){
19     int x=0,sig=1;char ch=getchar();
20     for(;!isdigit(ch);ch=getchar())if(ch=='-')sig=0;
21     for(;isdigit(ch);ch=getchar())x=10*x+ch-'0';
22     return sig?x:-x;
23 }
24 inline void write(int x){
25     if(x==0){putchar('0');return;}if(x<0)putchar('-'),x=-x;
26     int len=0,buf[15];while(x)buf[len++]=x%10,x/=10;
27     for(int i=len-1;i>=0;i--)putchar(buf[i]+'0');return;
28 }
29 void init(){
30     scanf("%s%s",s,t);
31     if(dfs(0,strlen(s)-1,0,strlen(t)-1))puts("YES");
32     else puts("NO");
33     return;
34 }
35 void work(){
36     return;
37 }
38 void print(){
39     return;
40 }
41 int main(){init();work();print();return 0;}
原文地址:https://www.cnblogs.com/chxer/p/4673371.html