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. 【LOJ 2144】「SHOI2017」摧毁「树状图」

    LOJ 2144 84pts 首先\(op2\)很简单.直接并查集一搞就好了(话说我现在什么东西都要写个并查集有点...) 然后\(op0\)我不会,就直接\(O(n^2)\)枚举一下\(P\)这个人 ...

  2. python内建的命名空间研究

    python内建的命名空间研究 说明: python内置模块的命名空间.python在启动的时候会自动为我们载入很多内置的函数.类,比如 dict,list,type,print,这些都位于 __bu ...

  3. mongodb java3.2驱动 测试 一些记录

    mongo驱动包 自带线程池的概念 获取 MongoClient mongoClient 后 通过客户端(mongoClient ) 获取 库操作 MongoDatabase 获取 表操作 Mongo ...

  4. CC2541之串口调试PM2.5传感器

    1. CC2541通过串口和PM25设备PMS7003通信,串口9600波特率,手机APP显示数据一直是128,先检查蓝牙数据通路问题,数据通路没问题 2. 看下串口是否OK,串口也不通,看到宏定义Z ...

  5. 网络应用简记(4):DNS使用

    dns,domain name system,域名系统,把域名转化成ip的系统. 先来看几上工具的使用,这几个工具都能把域名转换成ip,都使用了dns.dns就好比数据库,通过对它的查询,能给url找 ...

  6. 苹果 icloud 把我 ipad min 所有照片丢失

    苹果 icloud 把我 ipad min 所有照片丢失,大概发生在 '云上贵州' 之后! 发帖纪念--- 求个说法---

  7. Linux 命令(二)

    man help:线上查询及帮助命令 命令  --help:简单帮助 help  cd:查看一些Linux命令行的一些内置命令 文件和目操作命令(19个) ls  cd  cp  find  mkdi ...

  8. python札记

    进制转换 num = "0011"v = int(num, base=16)print(v)2->16

  9. mysql常用命令小结

    1.命令行中键入 net start/stop mysql 开启/停止mysql服务2.命令行中键入 mysql -u用户名 -p密码 连接数据库 (以下命令后须加分号';')3.用show语句显示当 ...

  10. java 代理模式(静态代理、动态代理、Cglib代理) 转载

    Java的三种代理模式 1.代理模式 代理(Proxy)是一种设计模式,提供了对目标对象另外的访问方式;即通过代理对象访问目标对象.这样做的好处是:可以在目标对象实现的基础上,增强额外的功能操作,即扩 ...