Equivalent Strings

CodeForces - 559B

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

sol:显然是分治,Hash判断字符串是否相等

不知道为什么一直TLE,至今仍然死在第91个点,弃疗了。

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-'); ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^); ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<)
{
putchar('-'); x=-x;
}
if(x<)
{
putchar(x+''); return;
}
write(x/);
putchar((x%)+'');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
const ll Power=,Mod=;
int n;
char S[][N];
ll Hash[][N],Base[N];
inline ll Calc(int l,int r,int o)
{
return (Hash[o][r]-Hash[o][l-]+Mod)%Mod*Base[n-r]%Mod;
}
inline bool Equal(int l1,int r1,int l2,int r2)
{
if(Calc(l1,r1,)==Calc(l2,r2,)) return ;
if((r1-l1+)&) return ;
if(Equal(l1,(l1+r1)>>,l2,(l2+r2)>>)&&Equal(((l1+r1)>>)+,r1,((l2+r2)>>)+,r2)) return ;
if(Equal(l1,(l1+r1)>>,((l2+r2)>>)+,r2)&&Equal(((l1+r1)>>)+,r1,l2,(l2+r2)>>)) return ;
return ;
}
int main()
{
freopen("data.in","r",stdin);
int i,j;
scanf("%s%s",S[]+,S[]+);
n=strlen(S[]+);
Base[]=;
for(i=;i<;i++)
{
Hash[i][]=;
for(j=;j<=n;j++)
{
Base[j]=1ll*Base[j-]*Power%Mod;
Hash[i][j]=1ll*(Hash[i][j-]+S[i][j]*Base[j]%Mod)%Mod;
}
}
if(Equal(,n,,n)) puts("YES");
else puts("NO");
return ;
}
/*
Input
aaba
abaa
Output
YES Input
aabb
abab
Output
NO Input
a
a
Output
YES
*/

codeforces559B的更多相关文章

随机推荐

  1. oracle 创建表空间TABLESPACE

    题外话: 在oracle中,我们的数据库用户都有一个默认表空间归属,当在该用户下创建表或其他对象时默认会将其归属在默认表空间: 不排除后期修改了用户默认表空间的情况存在,此后新加入的对象默认会放置在新 ...

  2. mongodb .explain('executionStats') 查询性能分析(转)

    mongodb性能分析方法:explain() 为了演示的效果,我们先来创建一个有200万个文档的记录.(我自己的电脑耗了15分钟左右插入完成.如果你想插更多的文档也没问题,只要有耐心等就可以了.) ...

  3. 圆角矩形shader

    在游戏中,有时需要对一张矩形图片进行切割,绘制成圆角矩形. circelrect.vert attribute vec4 a_position; attribute vec4 a_normal; at ...

  4. [翻译] C# 8.0 新特性

    原文: Building C# 8.0 [译注:原文主标题如此,但内容大部分为新特性介绍,所以意译标题为 "C# 8.0 新特性"] C# 的下一个主要版本是 8.0.我们已经为它 ...

  5. Java 将两个Map对象合并为一个Map对象

    实现方式是通过 putAll() 方法将多个 map 对象中的数据放到另外一个全新的 map 对象中,代码如下所示,展示了两个 map 对象的合并,如果是多个 map 合并也是用这种方式. publi ...

  6. Python全栈开发之路 【第二篇】:Python基础之数据类型

    本节内容 一.字符串 记住: 有序类型:列表,元组,字符串 ---> 都可迭代: 无序类型:字典,集合 ---> 不可迭代: 特性:不可修改 class str(object): &quo ...

  7. A short Glimpse to Spectral Sequences 快速入坑谱序列(英文)

    In this short paper with a few of page (but a lot of language mistake), I want to introduce the theo ...

  8. Day2 Python基础之基本操作(一)

    1.常用命令 调用cmd窗口 Win+R cmd命令窗口清屏 cls cmd命令窗口在运行python时清屏 import os i=os.system('cls') cmd命令窗口在运行python ...

  9. PS绘制扁平化风格相机镜头UI图标

    一.新建一个画布,绘制一个460*460图层,圆角半径40像素.填充渐变颜色,加一点点投影,这样就有质感了. 二.接下来我们要来绘制主体部分,绘制一个圆,大小400*400,用内阴影,渐变叠加,投影得 ...

  10. PEP 8 python编程规范

    一 代码编排 缩进.4个空格的缩进(编辑器都可以完成此功能),不使用Tap,更不能混合使用Tap和空格. 每行最大长度79,换行可以使用反斜杠,最好使用圆括号.换行点要在操作符的后边敲回车. 类和to ...