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. JPA和分布式事务简介

    1. Transaction 分两种,Local Transaction 和 Global Transaction. 涉及到一个Connection的Commit,称为Local Transactio ...

  2. iOS开发之CoreImage

    CoreImage是iOS中的一个图像处理框架,提供了强大高效的图像处理功能,可以通过调用简单的API来使用框架所带的各种滤镜对图像进行处理. CoreImgae的三个重要组成部分:1.CIFiter ...

  3. 使用python进行utf9编码和解码

    在2005年4月1日(也就是愚人节),IEEE的rfc4042文件规定了utf9和utf18这2个所谓的Unicode的高效转换格式. 具体的格式说明,有兴趣的话点击上面的rfc4042链接去观看. ...

  4. JSOUP如何优秀的下载JPEG等二进制图像

    引言 JSOUP默认是不支持解析JPEG等二进制图像的,解决方法也很简单,只需要加上Jsoup.ignoreContentType(true)这一行代码就可以.关于这一点的原因,来看看官方API说明. ...

  5. pycharm 安装和首次使用

    由于本人也是开发语言的学习小白,所以对于开发时使用的工具也不算太熟悉,不过这里还是要推荐一下本人使用过 的两款工具: 1.pycharm  :2.eclipse 这是我用的比较顺手的两个工具当然这两个 ...

  6. Git_GitHub详解

    Git和Github详细教程 一  概述 说到Git和Github,前几天我们知道微软以75亿美元收购全球最大的代码托管和写作平台GitHub,而GitHub是全球最大的代码仓库,很多开发人员都将代码 ...

  7. kuangbin 基础DP集合

    HDU 1024第一遍水过,没有体会到这个题的奥妙,思考了很久终于体会了.大概意思是求把序列分成m段的子序列,并不一定要覆盖完,求子序列和的最大值我们首先要写出基本的动态转移方程: DP:dp[ i ...

  8. iOS- 利用AFNetworking(AFN) - 实现文件断点下载

    https://www.cnblogs.com/qingche/p/3500746.html 1. 定义一个全局的AFHttpClient:包含有 1> baseURL 2> 请求 3&g ...

  9. [2017BUAA软工助教]团队alpha得分总表

    一.累计得分 项目 介绍 采访 贡献分 功能 技术 α例会 α发布 α测试 α展示 α事后 合计 满分 10 10 10 10 10 50 10 10 150 10 280 hotcode5 10 9 ...

  10. Memcached 集群架构与memcached-session-manager

    Memcached 集群架构方面的问题_知识库_博客园https://kb.cnblogs.com/page/69074/ memcached-session-manager配置 - 学习中间件调优管 ...