暴力,DFS,比较字符串

 

题意:

         

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:

  1. They are equal.
  2. 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:
    1. a1 is equivalent to b1, and a2 is equivalent to b2
    2. 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

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".

 

思路分析:

               使用DFS。

              明白几点:

             A:字符串长度为奇数时直接比较两个字符串相不相等

             B:字符串长度为偶数时,平分字符串,像题目的案例那样比较。如果不相等继续分,一直到字符串长度为奇数时,看A。

              1、首先比较两个字符串是否完全一样,用strncmp函数。相等就直接输出YES

              2、不相等就判断字符串的长度,分偶数跟奇数,如A跟B所述

              3、拆分好就比较,具体见代码

 

 

源代码:

 

 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 using namespace std;
 5 const int m = 200010;
 6 char s1[m], s2[m];
 7 bool dfs(char *a,char *b,int n)
 8 {
 9     if (strncmp(a, b, n) == 0)              //比较字符串
10         return true;
11     if (n % 2 != 0)                          //判断字符串长度为偶数还是奇数
12         return false;
13     int len = n / 2;                        //平分字符串
14     
15     if (dfs(a, b + len, len) && dfs(a + len, b, len))
16         return true;                                    
17     if (dfs(a, b, len) && dfs(a + len, b + len, len))    //拆分后的字符串比较
18         return true;
19     return false;
20 }
21 int main()
22 {
23       cin >> s1;
24       cin >> s2;
25      
26       cout <<( dfs(s1, s2, strlen(s1)) ? "YES" : "NO") << endl;
27     
28     return 0;
29 }

 

心得:

         按照自己以前的思路写,就是一大堆循环,然后自己也被弄晕了。这个题借用了别人的写法,觉得写得不错,简洁易懂,应该多学习,加油~~~

            但是提交时下面两句换一下就会超时。。。至今还没弄明白。。。。

1 if (dfs(a, b + len, len) && dfs(a + len, b, len))
2         return true;                                    
3     if (dfs(a, b, len) && dfs(a + len, b + len, len))    //拆分后的字符串比较
4         return true;

 

 

 

 

          

 

 

 

posted @ 2015-07-29 20:37  白一  阅读(311)  评论(0编辑  收藏  举报