CodeForces - 560D Equivalent Strings

Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a 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
aaba
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".

OJ-ID:
CodeForce-560D
author:
Caution_X

date of submission:
20191026

tags:
dfs

description modelling:
判断两个字符串是否“相等”。
相等:
(1)a=b
(2)a分成长度相等的两份a1,a2,b分成长度相等的两份b1,b2,并满足a1=b1&&a2=b2或者a1=b2&&a2=b1(语法上满足(string)a=(string)a1+(string)a2)。

major steps to solve it:
(1)裸dfs无剪枝

AC code:

#include<bits/stdc++.h>
using namespace std;
bool dfs(string a,string b)
{
    int lena=a.length(),lenb=b.length();
    string ta1=a.substr(0,lena/2),ta2=a.substr(lena/2,lena/2);
    string tb1=b.substr(0,lenb/2),tb2=b.substr(lenb/2,lenb/2);
    if(a==b)    return true;
    if(a.length()%2!=0)    return false;
    if(dfs(ta1,tb2)&&dfs(ta2,tb1))    return true;
    if(dfs(ta1,tb1)&&dfs(ta2,tb2))    return true;
    return false;    
}
int main()
{
    string a,b;
    cin>>a>>b;
    if(a==b)    puts("YES");
    else if(a.length()!=b.length())    puts("NO");
    else if(dfs(a,b))    puts("YES");
    else puts("NO");
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/cautx/p/11746179.html