One of Arkady's friends works at a huge radio telescope. A few decades ago the telescope has sent a signal s s towards a faraway galaxy. Recently they've received a response t t which they believe to be a response from aliens! The scientists now want to check if the signal t t is similar to s s .

The original signal s s was a sequence of zeros and ones (everyone knows that binary code is the universe-wide language). The returned signal t t , however, does not look as easy as s s , but the scientists don't give up! They represented t t as a sequence of English letters and say that t t is similar to s s if you can replace all zeros in s s with some string r 0  r0 and all ones in s s with some other string r 1  r1 and obtain t t . The strings r 0  r0 and r 1  r1 must be different and non-empty.

Please help Arkady's friend and find the number of possible replacements for zeros and ones (the number of pairs of strings r 0  r0 and r 1  r1 ) that transform s s to t t .

Input

The first line contains a string s s (2≤|s|≤10 5  2≤|s|≤105 ) consisting of zeros and ones — the original signal.

The second line contains a string t t (1≤|t|≤10 6  1≤|t|≤106 ) consisting of lowercase English letters only — the received signal.

It is guaranteed, that the string s s contains at least one '0' and at least one '1'.

Output

Print a single integer — the number of pairs of strings r 0  r0 and r 1  r1 that transform s s to t t .

In case there are no such pairs, print 0 0 .

Examples

Input
01
aaaaaa
Output
4
Input
001
kokokokotlin
Output
2

题意:给定一个01串S,和一个字符串T,然后问有多少种方案,使得0和1分别代表一段不同的字符串,使得S==T。

思路:假设S中0的个数的B,1的个数是A,那么就是问多少解Ax+By=C;枚举x,然后hash验证即可。

这里写了几个版本都WA17了。

版本1:双hash,用unsigned int自然溢出。

#include<bits/stdc++.h>
#define ui unsigned int
#define pii pair<ui,ui>
#define F first
#define S second
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int seed1=;
const int seed2=;
char a[maxn],b[maxn];
pii p[maxn],Hash[maxn]; int A,B;
int main()
{
int N,M,ans=;
scanf("%s%s",a+,b+);
N=strlen(a+); M=strlen(b+);
rep(i,,N) if(a[i]=='') A++;else B++;
p[].F=p[].S=; rep(i,,M) p[i].F=p[i-].F*seed1,p[i].S=p[i-].S*seed2;
Hash[].F=Hash[].S=;rep(i,,M) Hash[i].F=Hash[i-].F*seed1+b[i],Hash[i].S=Hash[i-].S*seed2+b[i];
rep(i,,M/A){
int x=i,y=(M-x*A)/B; bool F=true;
if(y<||x*A+y*B!=M) continue;
int vis1=-,vis0=-,pos=; pii tag0,tag1;
rep(j,,N){
if(a[j]=='') {
int To=pos+x-; pii tmp;
tmp.F=Hash[To].F-Hash[pos-].F*p[x].F;
tmp.S=Hash[To].S-Hash[pos-].S*p[x].S;
if(vis1==-) vis1=,tag1=tmp;
else if(tmp!=tag1) {F=false; break;}
pos=To+;
}
else {
int To=pos+y-; pii tmp;
tmp.F=Hash[To].F-Hash[pos-].F*p[y].F;
tmp.S=Hash[To].S-Hash[pos-].S*p[y].S;
if(vis0==-) vis0=,tag0=tmp;
else if(tmp!=tag0) {F=false; break;}
pos=To+;
}
}
if(tag0!=tag1&&F) ans++;
}
printf("%d\n",ans);
return ;
}

版本2:双hash,用unsigned long long自然溢出

#include<bits/stdc++.h>
#define ui unsigned long long
#define pii pair<ui,ui>
#define F first
#define S second
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const ui seed1=;
const ui seed2=;
char a[maxn],b[maxn];
pii p[maxn],Hash[maxn]; int A,B;
int main()
{
int N,M,ans=;
scanf("%s%s",a+,b+);
N=strlen(a+); M=strlen(b+);
rep(i,,N) if(a[i]=='') A++; else B++;
p[].F=p[].S=; rep(i,,M) p[i].F=p[i-].F*seed1,p[i].S=p[i-].S*seed2;
Hash[].F=Hash[].S=;rep(i,,M) Hash[i].F=Hash[i-].F*seed1+b[i],Hash[i].S=Hash[i-].S*seed2+b[i];
rep(i,,M/A){
int x=i,y=(M-x*A)/B; bool Flag=true;
if(y<||x*A+y*B!=M) continue;
int vis1=-,vis0=-,pos=; pii tag0,tag1;
rep(j,,N){
if(a[j]=='') {
int To=pos+x-; pii tmp;
tmp.F=Hash[To].F-Hash[pos-].F*p[x].F;
tmp.S=Hash[To].S-Hash[pos-].S*p[x].S;
if(vis1==-) vis1=,tag1=tmp;
else if(tmp!=tag1) {Flag=false; break;}
pos=To+;
}
else {
int To=pos+y-; pii tmp;
tmp.F=Hash[To].F-Hash[pos-].F*p[y].F;
tmp.S=Hash[To].S-Hash[pos-].S*p[y].S;
if(vis0==-) vis0=,tag0=tmp;
else if(tmp!=tag0) {Flag=false; break;}
pos=To+;
}
}
if(tag0!=tag1&&Flag) ans++;
}
printf("%d\n",ans);
return ;
}

以及各种换种子; 一个用uint,一个用ull; hash时一个直接加,一个加a-48;都是WA。

https://codeforces.com/blog/entry/4898

这里介绍了自然溢出容易被hack,和种子无关。%质数比较保险。 改一下就AC了。以后再也不自然溢出了。

#include<bits/stdc++.h>
#define ui long long
#define ul long long
#define pii pair<ui,ul>
#define F first
#define S second
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const ui seed1=;
const ul seed2=;
char a[maxn],b[maxn];
const ui Mod=1e9+;
pii p[maxn],Hash[maxn]; int A,B;
int main()
{
int N,M,ans=;
scanf("%s%s",a+,b+);
N=strlen(a+); M=strlen(b+);
rep(i,,N) if(a[i]=='') A++; else B++;
p[].F=p[].S=; rep(i,,M) p[i].F=p[i-].F*seed1%Mod,p[i].S=p[i-].S*seed2%Mod;
Hash[].F=Hash[].S=;rep(i,,M) Hash[i].F=(Hash[i-].F*seed1%Mod+b[i])%Mod,Hash[i].S=(Hash[i-].S*seed2%Mod+b[i])%Mod;
rep(i,,M/A){
int x=i,y=(M-x*A)/B; bool Flag=true;
if(y<||x*A+y*B!=M) continue;
int vis1=-,vis0=-,pos=; pii tag0,tag1;
rep(j,,N){
if(a[j]=='') {
int To=pos+x-; pii tmp;
tmp.F=((Hash[To].F-Hash[pos-].F*p[x].F%Mod)%Mod+Mod)%Mod;
tmp.F=((Hash[To].S-Hash[pos-].S*p[x].S%Mod)%Mod+Mod)%Mod;
if(vis1==-) vis1=,tag1=tmp;
else if(tmp!=tag1) {Flag=false; break;}
pos=To+;
}
else {
int To=pos+y-; pii tmp;
tmp.F=((Hash[To].F-Hash[pos-].F*p[y].F%Mod)%Mod+Mod)%Mod;
tmp.F=((Hash[To].S-Hash[pos-].S*p[y].S%Mod)%Mod+Mod)%Mod;
if(vis0==-) vis0=,tag0=tmp;
else if(tmp!=tag0) {Flag=false; break;}
pos=To+;
}
}
if(tag0!=tag1&&Flag) ans++;
}
printf("%d\n",ans);
return ;
}

CF1056:Check Transcription(被hack的hash)的更多相关文章

  1. Codeforces1056E.Check Transcription(枚举+Hash)

    题目链接:传送门 题目: E. Check Transcription time limit per test seconds memory limit per test megabytes inpu ...

  2. [CF1056E]Check Transcription

    题目:Check Transcription 传送门:http://codeforces.com/contest/1056/problem/E 分析: 1)显然有个$O( \frac{t}{max(c ...

  3. CodeForces 1056E - Check Transcription - [字符串hash]

    题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...

  4. CF1056E Check Transcription 字符串哈希

    传送门 暴力枚举\(0\)的长度,如果对应的\(1\)的长度也是一个整数就去check是否合法.check使用字符串哈希. 复杂度看起来是\(O(st)\)的,但是因为\(01\)两个数中数量较多的至 ...

  5. codeforces gym 101164 K Cutting 字符串hash

    题意:给你两个字符串a,b,不区分大小写,将b分成三段,重新拼接,问是否能得到A: 思路:暴力枚举两个断点,然后check的时候需要字符串hash,O(1)复杂度N*N: 题目链接:传送门 #prag ...

  6. puppeteer(五)chrome启动参数列表API

    List of Chromium Command Line Switches https://peter.sh/experiments/chromium-command-line-switches/ ...

  7. CEF 支持的命令行参数

    参考:https://peter.sh/experiments/chromium-command-line-switches/ List of Chromium Command Line Switch ...

  8. Capabilities & ChromeOptions

    https://sites.google.com/a/chromium.org/chromedriver/capabilities http://stackoverflow.com/questions ...

  9. List of Chromium Command Line Switches(命令行开关集)——官方指定命令行更新网址

    转自:http://peter.sh/experiments/chromium-command-line-switches/ There are lots of command lines which ...

随机推荐

  1. HTML5开发——轻量级JSON存储解决方案Lawnchair.js

    Lawnchair是一个轻量级的移动应用程序数据持久化存储方案,同时也是客户端JSON文档存储方法,优点是短小,语法简洁,扩展性比较好. 现在做HTML5移动应用除了LocalStorage的兼容性比 ...

  2. svn命令行使用

    1.将文件checkout到本地目录    svn checkout path(path是服务器上的目录)    例如:svn checkout svn://192.168.1.1/pro/domai ...

  3. Jquery 简明介绍

    http://www.cnblogs.com/luotianshuai/p/5196997.html http://www.cnblogs.com/liujianzuo888/articles/568 ...

  4. 39XML文档类

    Xml源代码 domxml.h #ifndef DOMXML_H #define DOMXML_H #include <QString> #include <QStringList& ...

  5. 33Sql数据删除与遍历

    数据库的创建.添加.修改.查询.删除都是利用SQL语句和类QSqlQuery的结合. QSqlDatabase::database().可返回当前正在打开的数据库对象. 数据库的删除 //获取删除的名 ...

  6. 大喜python版opencv3发布,demo脚本抢鲜版发布

    大喜,python版opencv3发布 zwPython3的升级也可以启动了,一直在等这个,zwPython会直接升级到版本3:zwPython3 zwPython3采用64位python3,支持op ...

  7. ZW网络团队及资源简介

    ZW网络团队及资源简介 ZW网络推广团队,是国内首个教父级网络营销团队,自1997年以来,先后参与操盘多个重大互联网项目,服务过超过150家国际500强客户,是微软公司首家官方认证的网络公关服务商,新 ...

  8. 【UI】android如何绘制一个饼图

    代码下载 需求 1:实心饼图,颜色填充百分比区域 2:带区域说明 3:饼图有阴影 思路:这个其实和绘制进度条原理差不多,都是360度根据所占百分比算出绘制弧度,然后调用canvas的画弧函数. 阴影其 ...

  9. postman 做接口测试

    Postman 之前是作为Chrome 的一个插件,现在要下载应用才能使用. 以下是postman 的界面: 各个功能区的使用如下: 快捷区: 快捷区提供常用的操作入口,包括运行收藏夹的一组测试数据, ...

  10. 裸眼 3D 技术是什么原理?

    https://www.zhihu.com/question/19553745 作者:杨英东链接:https://www.zhihu.com/question/19553745/answer/1227 ...