标签:
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:
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
aaba abaa
YES
aabb abab
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 }
标签:
原文地址:http://www.cnblogs.com/huaxiangdehenji/p/4694335.html