Equivalent Strings

Equivalent Strings

题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/E

题意:

   给出两个字符串,确定是否相等。一个字符串分割成相同大小的两半a1和a2,另一个字符串分割成相同大小的成两半b1和b2。

以下是正确的:
a1相当于b1 ,a2相当于b2
a1相当于b2 ,a2相当于b1

 

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

分析:

求出字符串的长度l。

如果为奇数串,只能比较是否每个字符相同,

如果为偶数串,第一个串分成两个相等长度的串为a, a+l/2,

第二个串也分成b, b+l/2,判断a== b && a+l/2 == b+l/2 || a == b+l/1 && a+l/2 == b.


#include<iostream>
#include<cstring>
using namespace std;
const int maxn=200001;
char a[maxn],b[maxn];
int bj(char*a,char*b,int l)   //比较两个字符串是否相等
{
      for(int i=0;i<l;i++)
	  {  
        if(a[i]!=b[i]) 
			return 0;  
	  }  
    return 1;  
}
int Do(char*a,char*b,int l)
{    
      int f=0;
	if(bj(a,b,l)==1)
	  f=1;
      if(l%2==1)          //  奇字符串
		{
		if(f==1)return 1;
        else    return 0;
		}
	  else{                          //偶字符串
		  if(f==1)  return 1;
	          else	        
			  { 
              l=l/2;
	           if(Do(a,b,l)&&Do(a+l,b+l,l)||Do(a,b+l,l)&&Do(a+l,b,l))  //判断
	           return 1;
	             else return 0;
	
			  }
	  }
}
int main()
{
	cin>>a>>b;
   int  x=strlen(a);
	if(Do(a,b,x)==1)  
		cout<<"Yes"<<endl;
	else  cout<<"No"<<endl;
	return 0;
}

 

posted @ 2015-07-28 21:05  枫虹  阅读(215)  评论(0编辑  收藏  举报