码迷,mamicode.com
首页 > 其他好文 > 详细

HUAS Summer Trainning #3 E

时间:2015-08-02 19:57:29      阅读:623      评论:0      收藏:0      [点我收藏+]

标签:

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输出


题目大意:给你2个长度都为n的字符串,让你比较这2个字符串是否相同,(要分成相同长度的字符串,奇数直接比较)
例如第一个字符串可以分为A1,A2。第二个可以分为B1,B2。
当A1=B1且A2=B2,或者A1=B2且A2=B1时,输出YES,否则NO。
解题思路:如果字符串的长度为偶数,对半分直到字符串长度分为奇数,每次看是否满足条件,奇数直接进行比较。
(这里就需要用到递归。)
代码:
 1 #include<iostream>
 2 #include<string>
 3 #include<cstring>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=200000+100;
 7 char d[maxn],b[maxn];
 8 bool dfs(char* a,char* b,int n)
 9 {
10     if(!strncmp(a,b,n))
11         return  true;
12     if(n%2)
13         return false;
14      n=n/2;
15     if(dfs(a,b+n,n)&&dfs(a+n,b,n))
16         return true;
17     if(dfs(a,b,n)&&dfs(a+n,b+n,n))
18         return true;
19     return false;
20 }
21 int main()
22 {
23     cin>>d;
24     cin>>b;
25     if(dfs(d,b,strlen(d)))
26         cout<<"YES"<<endl;
27     else cout<<"NO"<<endl;
28     return 0;
29 }

 


HUAS Summer Trainning #3 E

标签:

原文地址:http://www.cnblogs.com/huaxiangdehenji/p/4694335.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!