Equivalent Strings

Description

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.

Sample Input

Input

aabaabaa

Output

YES

Input

aabbabab

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

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef unsigned long long LL;
 4 const int maxn=200010;
 5 const int B=123;
 6 char a[maxn],b[maxn];
 7 int cmp(int len,char x[],char y[])
 8 {
 9     for(int i=0;i<len;i++)
10         if(x[i]<y[i])return -1;
11         else if(x[i]>y[i])return 1;
12     return 0;
13 }
14 void get(int len,char *s)
15 {
16     if(len&1)return ;
17     len/=2;
18     get(len,s);
19     get(len,s+len);
20     if(cmp(len,s,s+len)>0)
21     {
22         for(int i=0;i<len;i++)
23             swap(s[i],s[i+len]);
24     }
25 }
26 int main()
27 {
28     while(scanf("%s%s",a,b)!=EOF)
29     {
30         get(strlen(a),a);
31         get(strlen(b),b);
32         if(strcmp(a,b)==0)printf("YES\n");
33         else printf("NO\n");
34     }
35     return 0;
36 }

一开始我也是在傻傻的暴力,然而并没有什么软用。。

仔细想想就知道会超时。。

其实我们只要按他的string比较规则把字符串      一层层比较然后排序,

在最后return  string a==string b 就行了

这样是不是剪短了简答树的很多子叶呢??

时间: 2024-08-24 05:11:00

Equivalent Strings的相关文章

D. Equivalent Strings

D. Equivalent Strings 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cmath> 5 #include <algorithm> 6 #include <string> 7 #include <vector> 8 #include <set> 9 #include <map>

[2016-03-23][codeforces][560][D][Equivalent Strings]

时间:2016-03-23 14:15:39 星期三 题目编号:[2016-03-23][codeforces][560][D][Equivalent Strings] 题目大意:定义两个字符串相等方式,给出两个字符串,问是否相等 分析:递归判断即可 遇到的问题:长度为奇数的字符串一定不相等 #include <iostream> #include <string> using namespace std; int issame(string str1,string str2){

Equivalent Strings (字符串相等?)

Equivalent Strings E - 暴力求解.DFS Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description Today on a lecture about strings Gerald learned a new definition of string equivalency. Two strings a and b of equal length are

Codeforces Round #313 (Div. 2) D. Equivalent Strings 解题心得

原题: Description 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: They are equal. If we split string a into two halves of the sam

Codeforces Round #313 (Div. 1) B.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: They are equal. If we split string a into two halves of the same size a1 and a2

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

运气不错,这次cf大涨,居然是房间第二(要不是D题TLE了,就第一了) 用string会TLE,用char加下标,还看到更牛的算法, 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxx=200010; 6 char a[maxx],b[maxx]; 7 bool cmp(int p1,int p2,int len) 8 {

Codeforces 559B Equivalent Strings 等价串

题意:给定两个等长串a,b,判断是否等价.等价的含义为:若长度为奇数,则必须是相同串.若长度是偶数,则将两串都均分成长度为原串一半的两个子串al,ar和bl,br,其中al和bl等价且ar和br等价,或者al和br等价且ar和bl等价. 实际上很水.直接按照题意模拟写个递归分治就可以求.比赛的时候总觉得这样暴力写会TLE,因为算了下大概是4^(log2(n))的复杂度,也就是n^2,所以比赛的时候就想了下,将两个串都按照题意转化为字典序最小串(循环节的最小表示法)然后比较a和b的两个最小表示法是

Codeforces Round #313 (Div. 2) D.Equivalent Strings (字符串)

感觉题意不太好懂 = =# 给两个字符串 问是否等价等价的定义(满足其中一个条件):1.两个字符串相等 2.字符串均分成两个子串,子串分别等价 因为超时加了ok函数剪枝,93ms过的. #include <iostream> #include <cstring> #define clr(x,c) memset(x,c,sizeof(x)) using namespace std; const int N = 200005; char s[N], t[N]; int sc[30],

Codeforces Round #313 (Div. 2) Equivalent Strings(搜索)

题目大意:判断两个字符串是否等价. 等价的条件(任意一条满足即可):1.两个字符串完全相同 2.把每个字符串分成长度相同的两部分,若a1等价于b1并且a2等价于b2,或者a1等价于b2并且a2等价于b1 由于等价的条件说得很模糊,我卡了不久.等价条件里的第2条的意思是,如果ab两个字符串的两两子串都满足这两个条件,那么ab是等价的(有点绕,对吧),如果我们都已经解读清楚这句话了,显然接下来我们可以递归判断等价了. 首先,如果两个字符串长度不相等,那么肯定不等价:如果当前字符串的长度是奇数,那么我