CF1056E Check Transcription 字符串哈希
暴力枚举\(0\)的长度,如果对应的\(1\)的长度也是一个整数就去check是否合法。check使用字符串哈希。
复杂度看起来是\(O(st)\)的,但是因为\(01\)两个数中数量较多的至少有\(\frac{|s|}{2}\)个,那么最多有\(\frac{2|t|}{|s|}\)个可能的答案,而每一次check是\(O(|s|)\)的,所以总复杂度是\(O(|t|)\)的
#include<bits/stdc++.h>
#define ll long long
#define PLL pair < ll , ll >
//This code is written by Itst
using namespace std;
inline int read(){
int a = 0;
char c = getchar();
bool f = 0;
while(!isdigit(c) && c != EOF){
if(c == '-')
f = 1;
c = getchar();
}
if(c == EOF)
exit(0);
while(isdigit(c)){
a = (a << 3) + (a << 1) + (c ^ '0');
c = getchar();
}
return f ? -a : a;
}
const int seed = 13331 , MOD1 = 1e9 + 7 , MOD2 = 1e9 + 9;
char s1[100010] , s2[1000010];
int L1 , L2 , cnt0 , cnt1 , cnt;
ll Hash[1000010][2] , poww[1000010][2];
void input(){
scanf("%s %s" , s1 + 1 , s2 + 1);
L1 = strlen(s1 + 1);
L2 = strlen(s2 + 1);
}
void init(){
for(int i = 1 ; i <= L1 ; ++i)
s1[i] == '0' ? ++cnt0 : ++cnt1;
poww[0][0] = poww[0][1] = 1;
for(int i = 1 ; i <= L2 ; ++i){
Hash[i][0] = (Hash[i - 1][0] * seed + s2[i]) % MOD1;
Hash[i][1] = (Hash[i - 1][1] * seed + s2[i]) % MOD2;
poww[i][0] = poww[i - 1][0] * seed % MOD1;
poww[i][1] = poww[i - 1][1] * seed % MOD2;
}
}
inline PLL calcHash(int i , int j){
return PLL((Hash[i + j - 1][0] - Hash[i - 1][0] * poww[j][0] % MOD1 + MOD1) % MOD1 , (Hash[i + j - 1][1] - Hash[i - 1][1] * poww[j][1] % MOD2 + MOD2) % MOD2);
}
void work(){
for(int i = 1 ; i * cnt0 < L2 ; ++i)
if(!((L2 - i * cnt0) % cnt1)){
int p = 1 , j = (L2 - i * cnt0) / cnt1;
bool f = 1;
PLL Hash0 = PLL(-1,0) , Hash1 = PLL(-1,0);
for(int k = 1 ; f && k <= L1 ; ++k)
if(s1[k] == '0'){
if(Hash0.first == -1)
Hash0 = calcHash(p , i);
else
if(Hash0 != calcHash(p , i))
f = 0;
p += i;
}
else{
if(Hash1.first == -1)
Hash1 = calcHash(p , j);
else
if(Hash1 != calcHash(p , j))
f = 0;
p += j;
}
cnt += f && (Hash0 != Hash1);
}
}
void output(){
cout << cnt;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("in" , "r" , stdin);
//freopen("out" , "w" , stdout);
#endif
input();
init();
work();
output();
return 0;
}
CF1056E Check Transcription 字符串哈希的更多相关文章
- [CF1056E]Check Transcription
题目:Check Transcription 传送门:http://codeforces.com/contest/1056/problem/E 分析: 1)显然有个$O( \frac{t}{max(c ...
- CodeForces 1056E - Check Transcription - [字符串hash]
题目链接:https://codeforces.com/problemset/problem/1056/E One of Arkady's friends works at a huge radio ...
- 从Hash Killer I、II、III论字符串哈希
首先,Hash Killer I.II.III是BZOJ上面三道很经典的字符串哈希破解题.当时关于II,本人还琢磨了好久,但一直不明白为啥别人AC的代码都才0.3kb左右,直到CYG神犇说可以直接随机 ...
- cf_514C(字符串哈希)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=121897#problem/G Watto and Mechanism Time ...
- Codeforces1056E.Check Transcription(枚举+Hash)
题目链接:传送门 题目: E. Check Transcription time limit per test seconds memory limit per test megabytes inpu ...
- 【CodeForces】961 F. k-substrings 字符串哈希+二分
[题目]F. k-substrings [题意]给定长度为n的串S,对于S的每个k-子串$s_ks_{k+1}...s_{n-k+1},k\in[1,\left \lceil \frac{n}{2} ...
- HDU6599 (字符串哈希+回文自动机)
题意: 求有多少个回文串的前⌈len/2⌉个字符也是回文串.(两组解可重复)将这些回文串按长度分类,分别输出长度为1,2,...,n的合法串的数量. 题解:https://www.cnblogs.co ...
- 字符串哈希——1056E
典型的字符串哈希题 字符串hash[i]:子串s[1-i]代表的值 要截取串s[l-r] 求hash[r]-hash[l-1]*F[len]即可 #include<bits/stdc++.h& ...
- poj 2774 字符串哈希求最长公共子串
Long Long Message #include <iostream> #include <algorithm> #include <cstdio> #incl ...
随机推荐
- 【读书笔记】iOS-发布你的应用
一,添加图标 你的应用在iPhone主屏幕上的标准图标(Icon.png)是57像素*57像素的正方形,PNG格式,不能有透明效果或者图层,72DPI.除些之外,你还可以提供一个同样格式的114像素* ...
- “一切都是消息”--iMSF(即时消息服务框架)之【发布-订阅】模式
MSF的名字是 Message Service Framework 的简称,由于目前框架主要功能在于处理即时(immediately)消息,所以iMSF就是 immediately Message S ...
- loadrunner 运行场景-Controller及Load Generators宿主主机优化
运行场景-Controller及Load Generators宿主主机优化 by:授客 QQ:1033553122 1. Load Generator宿主主机优化设置 win7以下:进入“控制面板” ...
- 安卓开发_浅谈DatePicker(日期选择器)
DatePicker继承自FrameLayout类,日期选择控件的主要功能是向用户提供包含年.月.日的日期数据并允许用户对其修改.如果要捕获用户修改日期选择控件中的数据事件,需要为DatePicker ...
- Fiddler抓包使用教程-Https
转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/72956016 本文出自[赵彦军的博客] 开启 Https 抓包 Fiddler 默 ...
- 【redis专题(4)】命令语法介绍之sorted_set
有序集合可以模拟优先级队列的实现 增 zadd key score1 value1 score2 value2 .. redis 127.0.0.1:6379> zadd stu 18 lily ...
- MySQL8.0——Resource Group(资源组)
资源组介绍 简介 MySQL是单进程多线程的程序,MySQL线程包括后台线程(Master Thread.IO Thread.Purge Thread等),以及用户线程.在8.0之前,所有线程的优先级 ...
- 【PAT】B1067 试密码(20 分)
注意读取时的换行符用getchar吸收 第十个错误后直接输出锁定 #include<cstdio> #include<string.h> int main(){ char mi ...
- 15LaTeX学习系列之---LaTeX里插入数学公式
目录 目录 前言 (一)常用的数学公式命令 ==1.上下标== ==2.矢量== ==3.括号== ==4.符号关系== ==5.三角形符号== ==6.求和与累积== ==7.积分与微分== ==8 ...
- ghostscript远程代码执行漏洞复现
这两天网上ghostscript又又有漏洞信息了,但是没有poc,于是找找资料把今年8月21日的那个验证下 1.关于ghostscript Ghostscript是一套建基于Adobe.PostScr ...