题目链接:https://codeforces.com/problemset/problem/1056/E

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

The original signal $s$ was a sequence of zeros and ones (everyone knows that binary code is the universe-wide language). The returned signal $t$, however, does not look as easy as $s$, but the scientists don't give up! They represented $t$ as a sequence of English letters and say that $t$ is similar to $s$ if you can replace all zeros in $s$ with some string $r_0$ and all ones in $s$ with some other string $r_1$ and obtain $t$. The strings $r_0$ and $r_1$ 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$ and $r_1$) that transform $s$ to $t$.

Input
The first line contains a string $s (2 \le |s| \le 10^5)$ consisting of zeros and ones — the original signal.

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

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

Output
Print a single integer — the number of pairs of strings $r_0$ and $r_1$ that transform $s$ to $t$.

In case there are no such pairs, print 0.

Examples
Input
01
aaaaaa
Output
4

Input
001
kokokokotlin
Output
2

Note
In the first example, the possible pairs $(r_0,r_1)$ are as follows:

"a", "aaaaa"
"aa", "aaaa"
"aaaa", "aa"
"aaaaa", "a"
The pair "aaa", "aaa" is not allowed, since $r_0$ and $r_1$ must be different.

In the second example, the following pairs are possible:

"ko", "kokotlin"
"koko", "tlin"

题意:

给定一个 $0,1$ 字符串 $s$,和一个小写字母组成的字符串 $t$。

现在要找出两个互不相同且非空的字符串 $r_0, r_1$,使得将 $s$ 中的 $0$ 全部替换成 $r_0$,将 $1$ 全部替换成 $r_1$ 后,即可得到 $t$。

要求输出不同二元组 $(r_0,r_1)$ 的数量。

题解:

不妨枚举 $r_0$ 的长度,这样一来,根据 $0,1$ 的数目,可以直接算出 $r_1$ 的长度。

然后遍历 $s$,看此时假设的 $r_0$ 和 $r_1$ 是否可以成立,这个可以对 $t$ 字符串hash之后 $O(1)$ 地求出每个 $0,1$ 转换出来的 $r_0,r_1$ 符不符合要求。

关于时间复杂度,表面上看起来时间复杂度为 $O(|s||t|)$ 的,但实际上由于要满足条件 $|t| = cnt_0 \cdot |r_0| + cnt_1 \cdot |r_1|$,可供选择的 $(|r_0|,|r_1|)$ 比 |t| 少很多。

而且,对于很多的 $(|r_0|,|r_1|)$,若其对应的 $(r_0,r_1)$ 并非可行解,其实很快就能判定出来,不需要 $O(|s|)$ 次hash。

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int P=;
const int M=1e7+;
const int maxs=1e5+;
const int maxt=1e6+; int lens,lent;
char s[maxs],t[maxt];
int tot0,tot1; ll pre[maxt],Ppow[maxt];
void prework(int len)
{
pre[]=;
Ppow[]=;
for(int i=;i<=len;i++)
{
pre[i]=pre[i-]*P+(t[i]-'a'+), pre[i]%=M;
Ppow[i]=Ppow[i-]*P%M;
}
}
inline ll Hash(int l,int r) {
return (pre[r]-pre[l-]*Ppow[r-(l-)]%M+M)%M;
}
int main()
{
scanf("%s%s",s+,t+); lens=strlen(s+), lent=strlen(t+);
tot0=tot1=;
for(int i=;i<=lens;i++) tot0+=(s[i]==''), tot1+=(s[i]==''); int ans=;
int len0,len1;
prework(lent);
for(len0=;len0<lent;len0++)
{
if(tot0*len0>=lent) break;
if((lent-tot0*len0)%tot1>) continue;
len1=(lent-tot0*len0)/tot1; bool ok=;
int cnt0=, cnt1=;
ll hash_r0=, hash_r1=;
for(int i=;i<=lens;i++)
{
if(s[i]=='')
{
int l=cnt0*len0+cnt1*len1+, r=l+len0-;
ll has=Hash(l,r);
if(hash_r0==) {
hash_r0=has;
} else {
if(hash_r0!=has) {
ok=; break;
}
}
cnt0++;
}
if(s[i]=='')
{
int l=cnt0*len0+cnt1*len1+, r=l+len1-;
ll has=Hash(l,r);
if(hash_r1==) {
hash_r1=has;
} else {
if(hash_r1!=has) {
ok=; break;
}
}
cnt1++;
}
if(hash_r0!= && hash_r1!= && hash_r0==hash_r1) {
ok=;
break;
}
}
if(ok) ans++;
}
printf("%d\n",ans);
}

注意:不要使用unsigned long long类型的溢出自动取模,会被卡掉;改为对大质数取模就能通过了。

CodeForces 1056E - Check Transcription - [字符串hash]的更多相关文章

  1. CF1056E Check Transcription 字符串哈希

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

  2. CodeForces 7D Palindrome Degree 字符串hash

    题目链接:点击打开链接 #include<stdio.h> #include<iostream> #include<string.h> #include<se ...

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

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

  4. CodeForces - 727E Games on a CD 字符串Hash

    题意:有n个单词,每个单词长度为k,顺时针将它们写成一个圆圈串.现在知道g个长度为k的单词,是否可以从这g个单词中选择n个形成这个圆圈串?如果有多个答案,任意输出一个. 思路 可以发现,如果枚举第一个 ...

  5. 【题解】 Codeforces Edu44 F.Isomorphic Strings (字符串Hash)

    题面戳我 Solution 我们按照每个字母出现的位置进行\(hash\),比如我们记录\(a\)的位置:我们就可以把位置表示为\(0101000111\)这种形式,然后进行字符串\(hash\) 每 ...

  6. codeforces gym 101164 K Cutting 字符串hash

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

  7. 【codeforces 514C】Watto and Mechanism(字符串hash)

    [题目链接]:http://codeforces.com/contest/514/problem/C [题意] 给你n个字符串; 然后给你m个询问;->m个字符串 对于每一个询问字符串 你需要在 ...

  8. Codeforces 898F - Restoring the Expression(字符串hash)

    898F - Restoring the Expression 思路:字符串hash,base是10,事实证明对2e64取模会T(也许ull很费时),对1e9+7取模. 代码: #include< ...

  9. 线段树 + 字符串Hash - Codeforces 580E Kefa and Watch

    Kefa and Watch Problem's Link Mean: 给你一个长度为n的字符串s,有两种操作: 1 L R C : 把s[l,r]全部变为c; 2 L R d : 询问s[l,r]是 ...

随机推荐

  1. linux popen()与system()的区别

    linux popen()与system()的区别 popen() 可以在调用程序和POSIX shell /usr/bin/sh 要执行的命令之间创建一个管道(请参阅sh-posix(1) ). p ...

  2. SSD卡对redis的影响

    原文地址:http://antirez.com/news/52 Hello! As promised today I did some SSD testing. The setup: a Linux ...

  3. [转载]从100PV到1亿级PV网站架构演变

    原文地址:http://www.uml.org.cn/zjjs/201307172.asp 一个网站就像一个人,存在一个从小到大的过程.养一个网站和养一个人一样,不同时期需要不同的方法,不同的方法下有 ...

  4. spring-mybatis-data-common程序级分表操作实例

    spring-mybatis-data-common-2.0新增分表机制,在1.0基础上做了部分调整. 基于机架展示分库应用数据库分表实力创建 create table tb_example_1( i ...

  5. c# 创建项目时提示:未能正确加载“microsoft.data.entity.design.bootstrappackage.。。。。

    google了下, 果然找到办法了.看有的说要卸载,再清理注册表,真心不愿意啊,这安装一次就得好长时间.还好找到了这篇博文,无需卸载重安装,很简单的解决的方式,具体见http://blog.sina. ...

  6. Mysql依赖库Boost的源码安装,linux下boost库的安装

      boost‘准标准库’安装过程.安装的是boost_1_60_0. (1)首先去下载最新的boost代码包,网址www.boost.org. (2)进入到自己的目录,解压: bzip2 -d bo ...

  7. I帧、P帧、B帧、GOP、IDR 和PTS, DTS之间的关系

    一.视频传输原理 视频是利用人眼视觉暂留的原理,通过播放一系列的图片,使人眼产生运动的感觉.单纯传输视频画面,视频量非常大,对现有的网络和存储来说是不可接受的.为了能够使视频便于传输和存储,人们发现视 ...

  8. Mac下软件包管理器-homebrew

    类似于redhat系统的yum,ubuntu的apt-get,mac系统下也有相应的包管理容器:homebrew.用法与apt-get.yum大同小异,都是对安装软件做一些安装删除类的命令行操作,以下 ...

  9. Java知多少(41)泛型详解

    我们知道,使用变量之前要定义,定义一个变量时必须要指明它的数据类型,什么样的数据类型赋给什么样的值. 假如我们现在要定义一个类来表示坐标,要求坐标的数据类型可以是整数.小数和字符串,例如: x = 1 ...

  10. Core Animation 动画的使用:关键帧动画、基础动画、动画组

    首先让我们了解下什么是 Core Animation,Core Animation 为核心动画,他为图形渲染和动画提供了基础.使用核心动画,我们只需要设置起点.终点.关键帧等一些参数,剩下的工作核心动 ...