CodeForces - 559B Equivalent Strings 分治,递归

Today on a lecture about strings Gerald learned a new definition of string equivalency. Two stringsa and b of equal length are called equivalent 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.

Examples
Input
abaa
Output
YES
Input
aabb
abab
Output
NO
Note

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


题意: 给二个字符串s1,s2 每个等分成二段,要交叉相等或者对应相等,(这里的相等可以理解为长度一样并且字符完全一样或者字符再继续等分直到长度一样并且字符完全一样

思路: 重复处理,并且要等分,用分治

#include<bits\stdc++.h>
using namespace std;
#define  ll long long
#define  ull unsigned long long
#define mINF(a) memset(a,0x3f3f3f3f,sizeof(a))
#define m0(a) memset(a,0,sizeof(a))
#define mf1(a) memset(a,-1,sizeof(a))
const int INF=0x3f3f3f3f;
const int len=2e5+5;
const ll MAX=1e18;
string s1,s2;
bool dfs(int l1,int r1,int l2,int r2)
{
	int flag=0;
	for(int i=0;i<=r1-l1;++i)
	{
		if(s1[l1+i]!=s2[l2+i])
		{
			flag=1;
			break;
		}//判断这二段字符串是否相等 
	}
	if(!flag)return 1;//相等 
	if((r1-l1+1)%2)return 0;//在不等的情况下,字符串长度为奇数 
	int m1,m2;
	m1=(l1+r1)/2;m2=(l2+r2)/2;
	if(dfs(l1,m1,l2,m2)&&dfs(m1+1,r1,m2+1,r2))return 1; 
	if(dfs(l1,m1,m2+1,r2)&&dfs(m1+1,r1,l2,m2))return 1;
	return 0;
}
int main()
{
	cin>>s1>>s2;
	int l1=s1.size();
	int l2=s2.size();
	if(l1!=l2||!dfs(0,l1-1,0,l2-1))
		puts("NO");
	else puts("YES");
}

猜你喜欢

转载自blog.csdn.net/hutwuguangrong/article/details/80094235
今日推荐