Codeforces Round #313 (Div. 2)560D. Equivalent Strings

原创 2016年08月31日 10:10:23
D. Equivalent Strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

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.

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

#include <bits/stdc++.h>
using namespace std;
#define maxn 200010
char a[maxn], b[maxn];
bool dfs(char s1[], char s2[], int len){
    if(!strncmp(s1, s2, len)) return 1;
    if(len % 2 == 1) return 0;
    int m = len/2;
    if(dfs(s1+m, s2, m)&&dfs(s1, s2+m, m)) return 1;
    if(dfs(s1, s2, m)&&dfs(s1+m, s2+m, m)) return 1;
}
int main(){
    scanf("%s %s", a, b);
    int len = strlen(a);
    if(dfs(a, b, len)) printf("YES\n");
    else printf("NO\n");
}


版权声明:本文为博主原创文章,未经博主允许不得转载。

Codeforces Round #258 (Div. 2)-(A,B,C,D,E)

A:Game With Sticks 水题。。。每次操作,都会拿走一个横行,一个竖行。 所以一共会操作min(横行,竖行)次。 #include #include #include #include ...
  • rowanhaoa
  • rowanhaoa
  • 2014年07月25日 14:39
  • 1468

【解题报告】Codeforces Round #401 (Div. 2)

简略的解题报告。
  • TRiddle
  • TRiddle
  • 2017年02月25日 17:55
  • 408

Codeforces Round #395 (Div. 2)(ABCD)

ps:打完这场cf才知道自己真的很菜,还是停留在AB题的水平,有时候CD其实很简单,但就是想不到,别人一眼看出而我就是想不到,有时候想到了点子上但就是突破不了 题目链接:  Codeforc...
  • qq_34731703
  • qq_34731703
  • 2017年02月03日 12:50
  • 484

Codeforces Round #460 (Div. 2) D. Substring(拓扑排序)

题目链接题意:有一个n个点m条边的有向图,每个节点有一个字母,路径的权值是路径上相同字母的最大个数。求最大的路径权值。思路:因为只有26个字母,所以直接假设其中一个字母为相同字母数最大的字母。枚举每一...
  • cyf199775
  • cyf199775
  • 2018年02月01日 13:55
  • 33

Codeforces Round #378 (Div. 2) E. Sleep in Class

链接:http://codeforces.com/contest/733/problem/E 题意:给一个只含'U'和'D'的字符串,U表示向右走,D表示向左走。每个格子踩一次之后翻转(U变D,D变...
  • Fsss_7
  • Fsss_7
  • 2016年11月15日 13:48
  • 355

Codeforces Round #442 (Div. 2)

A. #include using namespace std; typedef long long ll; int main() { string s; cin >> s; ...
  • Egqawkq
  • Egqawkq
  • 2017年10月24日 02:15
  • 236

Codeforces Round #428 (Div. 2):D. Winter is here(组合数公式)

D. Winter is here time limit per test 3 seconds memory limit per test 256 megabytes input stan...
  • Jaihk662
  • Jaihk662
  • 2017年08月14日 14:52
  • 341

Codecraft-17 and Codeforces Round #391 (Div. 1 + Div. 2, combined)A+B

A. Gotta Catch Em’ All!time limit per test:1 secondmemory limit per test:256 megabytesinput:standard...
  • qq_33183401
  • qq_33183401
  • 2017年01月16日 17:12
  • 239

Codeforces Round #460 (Div. 2) D. Substring

题目链接:D. Substring 题目大意 一张有向图,每个结点上有一个字母,定义一条路径的权值为路径中出现次数最多的字母的出现次数,求图中权值最大的路径的权值,如果权值可以无限大,输出-1...
  • litmxs
  • litmxs
  • 2018年01月31日 23:25
  • 57

Codeforces Round #327 (div.2)(A B C D)

codeforces round #327 (div.2)
  • lincifer
  • lincifer
  • 2015年10月26日 13:11
  • 1085
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces Round #313 (Div. 2)560D. Equivalent Strings
举报原因:
原因补充:

(最多只允许输入30个字)