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. 一道很有意思的java线程题

    这几天看结城浩的<java多线程设计模式>,跟着做一些习题,有几道题目很有意思,记录下自己的体会. 首先是题目(在原书212页,书尾有解答): public class Main { pu ...

  2. Long类型参数传到前端精度丢失的解决方案

        由于公司数据库表的id是利用雪花算法生成的,所以实体类里面定义的数据类型为Long.但是这个数据传到前端时,发生了精度丢失的现象.本文记录了从java后端的角度如何解决这个精度丢失的问题,便于 ...

  3. <<.NET B/S 架构实践>> 几种概念区别 - 算法、设计模式、企业应用架构模式、架构模式

    算法:相信大家对算法肯定不陌生(但其实绝大多数开发人员对这个非常陌生且抗拒),因为从学校没毕业开始就已经被算法折磨了,哈哈 设计模式:爱学习的开发人员对这个也不会陌生,是些到了一定工作阶段必须学的思想 ...

  4. Centos7 下SVN迁移

    SVN迁移需要做如下操作: 1. 将原来的Repository导出 . #svnadmin dump 原有repos的目录路径 > dumpfile (不同服务器安装目录不同,根据具体情况调整) ...

  5. C. Edgy Trees

    链接 [https://codeforces.com/contest/1139/problem/C] 题意 给你n个点,n-1个边,无向的.有red和black的. k表示经过这k个点.可以跨其他点 ...

  6. R语言绘制箱型图

    箱形图是数据集中数据分布情况的衡量标准.它将数据集分为三个四分位数.盒形图表示数据集中的最小值,最大值,中值,第一四分位数和第四四分位数. 通过为每个数据集绘制箱形图, 比较数据集中的数据分布也很有用 ...

  7. java得到日期相减的天数

    /** * <li>功能描述:时间相减得到天数 * @param beginDateStr * @param endDateStr * @return * long * @author A ...

  8. python中的Init方法, new 方法 call 方法

    new 方法实现单列模式思考 class Single: _single = None _single_only = None def __init__(self, value): self.v = ...

  9. HTTP之referrer

    我们知道,在页面引入图片.JS 等资源,或者从一个页面跳到另一个页面,都会产生新的 HTTP 请求,浏览器一般都会给这些请求头加上表示来源的 Referrer 字段.Referrer 在分析用户来源时 ...

  10. oracle查看表结构命令desc