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".
分析步骤:
1.先判断两串字符串长度为奇数还是偶数,奇数则要遍历全部的两个字符串,字符和顺序都相同才能输出YES,否则输出NO。
2.如果是偶数,两个字符串完全相同就直接输出YES,否则将两个字符串各切成两个子字符串再比较。
代码如下:
1 #include<iostream>
2 #include<cstring>
3 using namespace std;
4 const int maxn=200005;
5 char a[maxn],b[maxn];
6
7 int i,flag=0;
8 int compare(char* a1,char* b1,int l) //自定义比较函数
9 {
10 for(i=0;i<l;i++)
11 {
12 if(a1[i]!=b1[i])
13 {
14 return 0;
15 }
16 }
17 flag=1;
18 return flag;
19 }
20
21
22 int fun(char* a1,char* b1,int l)
23 {
24 int k=0;
25 if(compare(a1,b1,l))
26 k=1;
27 if(l%2==1)
28 {
29 if(k==1)
30 return 1;
31 else
32 return 0;
33 }
34 else
35 {
36 if(k==1)
37 return 1;
38 else
39 {
40 l=l/2;
41 if( (fun(a1,b1,l) && fun(a1+l,b1+l,l)) || (fun(a1,b1+l,l) && fun(a1+l,b1,l)) )
42 return 1;
43 else
44 return 0;
45 }
46 }
47 }
48
49 int main()
50 {
51 cin>>a;
52 cin>>b;
53 int len=strlen(a);
54 if(fun(a,b,len))
55 cout<<"YES"<<endl;
56 else
57 cout<<"NO"<<endl;
58 return 0;
59 }