codeforces559B
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, and string binto two halves of the same size b1 and b2, then one of the following is correct:
- a1 is equivalent to b1, and a2 is equivalent to b2
- 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
aaba
abaa
YES
aabb
abab
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的更多相关文章
随机推荐
- Java多线程(三)—— synchronized关键字详解
一.多线程的同步 1.为什么要引入同步机制 在多线程环境中,可能会有两个甚至更多的线程试图同时访问一个有限的资源.必须对这种潜在资源冲突进行预防. 解决方法:在线程使用一个资源时为其加锁即可. 访问资 ...
- SkylineGlobe 7.0版本 矢量数据查询示例代码
在Pro7.0.0和7.0.1环境下测试可用. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...
- 4-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案数据篇(云端电脑(Windows)安装配置数据库,使用本地Navicat for MySQL和手机APP 远程连接测试)
3-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案数据篇(安装配置数据库,使用Navicat for MySQL和手机APP 连接测试) 根据前面的教程把软件复制到云 ...
- Docker镜像存储-overlayfs
一.概述 Docker中的镜像采用分层构建设计,每个层可以称之为“layer”,这些layer被存放在了/var/lib/docker/<storage-driver>/目录下,这里的st ...
- Linux ACL 权限
ACL 是什么 ACL的全称是 Access Control List (访问控制列表) ,一个针对文件/目录的访问控制列表.它在UGO权限管理的基础上为文件系统提供一个额外的.更灵活的权限管理机制. ...
- git 分支管理规范
保证master分支永远处于可部署的状态.禁止自接提交代码到master分支 开发分支基于master分支创建,命名规范如下: 如果是功能需求,分支命名为feature/xxx,xxx要具有描述性 如 ...
- MySQL 5.7中如何定位DDL被阻塞的问题
在上篇文章<MySQL表结构变更,不可不知的Metadata Lock>中,我们介绍了MDL引入的背景,及基本概念,从“道”的层面知道了什么是MDL.下面就从“术”的层面看看如何定位MDL ...
- 个人实战演练全过程——No.1 最大连续子数组求和
之前的一次个人总结和一次单元测试入门学习是开启软件工程课程的前奏曲,也是热身,现在大家对于这门课程也有了初步的了解和认识,这次要开始真正的演奏了,要从头到尾完全靠自己的能力来解决一个问题,进行实战演练 ...
- 常见的web攻击手段总结
xxs攻击(跨站脚本攻击) 攻击者在网页中嵌入恶意脚本程序,当用户打开该网页时脚本程序便在浏览器上执行,盗取客户端的cookie.用户名密码.下载执行病毒木马程 序 解决: 我们可以对用户输入的数据进 ...
- 并发连接MySQL
先吐槽一下libmysqlclientAPI的设计, 多个线程同时去connect居然会core掉. 后来Google了一番, 才发现mysql_real_connect不是线程安全的, 需要一些额外 ...