Codeforces 1090J $kmp+hash+$二分
题意
给出两个字符串\(s\)和\(t\),设\(S\)为\(s\)的任意一个非空前缀,\(T\)为\(t\)的任意一个非空前缀,问\(S+T\)有多少种不同的可能。
Solution
看了一圈,感觉好像就我一个人写的\(kmp+hash+\)二分。
直接算好像不是很好算?先容斥一下,不同\(=\)总方案\(-\)相同。
显然总方案为两个字符串的长度的乘积,考虑相同的情况怎么算。
相同即两组\(S\)和\(T\)不同,但\(S+T\)本质相同的情况.
这个东西怎么算呢。。。。

(感觉看图会好理解一点
不难想到当上图框出来的地方相同,则两者同质。
先来看右边那个框,显然这个东西就是一个字符串里两个子串\([1,i],[j,k]\)相同。
左边这个框就是\(s\)的某个子串和\(t\)的前缀相同。
具体怎么算?
根据上图,设\(a_i\)为\(t\)的前缀\([1,i]\)在\(s\)里出现了几次,这个可以\(hash+\)二分算。
设\(b_i\)为符合\([1,j]=[i-j+1,i]\)的\(j\)的最大值,这个可以\(kmp\)一波。
那么最终同质的个数就是\(\sum_{i=2}^{|t|}a_{i-b_i}\)
#include<bits/stdc++.h>
#define For(i,x,y) for (register int i=(x);i<=(y);i++)
#define Dow(i,x,y) for (register int i=(x);i>=(y);i--)
#define cross(i,u) for (register int i=first[u];i;i=last[i])
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
inline ll read(){
ll x=0;int ch=getchar(),f=1;
while (!isdigit(ch)&&(ch!='-')&&(ch!=EOF)) ch=getchar();
if (ch=='-'){f=-1;ch=getchar();}
while (isdigit(ch)){x=(x<<1)+(x<<3)+ch-'0';ch=getchar();}
return x*f;
}
const int N = 1e5+10;
int n,m;
char a[N],b[N];
const ull base = 233;
ull pre[N],Pre[N],p[N];
const ll Base = 23, mod = 1e9+7;
ll pre2[N],Pre2[N],p2[N];
inline void GetPre(){
p[0]=1;For(i,1,n) p[i]=p[i-1]*base;
For(i,1,n) pre[i]=pre[i-1]*base+a[i];
For(i,1,m) Pre[i]=Pre[i-1]*base+b[i];
p2[0]=1;For(i,1,n) p2[i]=p2[i-1]*Base%mod;
For(i,1,n) (pre2[i]=pre2[i-1]*Base%mod+a[i])%=mod;
For(i,1,m) (Pre2[i]=Pre2[i-1]*Base%mod+b[i])%=mod;
}
inline ull query(int l,int r){return pre[r]-pre[l-1]*p[r-l+1];}
inline ll query2(int l,int r){return (pre2[r]-pre2[l-1]*p2[r-l+1]%mod+mod)%mod;}
int now,fail[N];
inline void GetKmp(){
now=0;
For(i,2,m){
while (now&&b[now+1]!=b[i]) now=fail[now];
fail[i]=(b[now+1]==b[i]?++now:now);
}
}
int sum[N];
inline void Get(){
For(i,2,n){
int l=1,r=min(m,n-i+1),mid,ans=0;
while (l<=r){
mid=l+r>>1;
if (query(i,i+mid-1)==Pre[mid]&&query2(i,i+mid-1)==Pre2[mid]) l=mid+1,ans=mid;
else r=mid-1;
}
sum[ans]++;
}
sum[0]=0;
Dow(i,m,1) sum[i]+=sum[i+1];
}
inline void calc(){
ll ans=1ll*n*m;
For(i,2,m) if (fail[i]) ans-=sum[i-fail[i]];
printf("%lld\n",ans);
}
int main(){
scanf("%s",a+1),scanf("%s",b+1),n=strlen(a+1),m=strlen(b+1);
GetPre(),GetKmp(),Get(),calc();
}
Codeforces 1090J $kmp+hash+$二分的更多相关文章
- [Codeforces 1199C]MP3(离散化+二分答案)
[Codeforces 1199C]MP3(离散化+二分答案) 题面 给出一个长度为n的序列\(a_i\)和常数I,定义一次操作[l,r]可以把序列中<l的数全部变成l,>r的数全部变成r ...
- CodeForces 670D1 暴力或二分
今天,开博客,,,激动,第一次啊 嗯,,先来发水题纪念一下 D1. Magic Powder - 1 This problem is given in two versions that diff ...
- codeforces 895B XK Segments 二分 思维
codeforces 895B XK Segments 题目大意: 寻找符合要求的\((i,j)\)对,有:\[a_i \le a_j \] 同时存在\(k\),且\(k\)能够被\(x\)整除,\( ...
- Codeforces 626C Block Towers(二分)
C. Block Towers time limit per test:2 seconds memory limit per test:256 megabytes input:standard inp ...
- codeforces 803D Magazine Ad(二分+贪心)
Magazine Ad 题目链接:http://codeforces.com/contest/803/problem/D ——每天在线,欢迎留言谈论. 题目大意: 给你一个数字k,和一行字符 例: g ...
- Success Rate CodeForces - 807C (数学+二分)
You are an experienced Codeforces user. Today you found out that during your activity on Codeforces ...
- Codeforces 1132D - Stressful Training - [二分+贪心+优先队列]
题目链接:https://codeforces.com/contest/1132/problem/D 题意: 有 $n$ 个学生,他们的电脑有初始电量 $a[1 \sim n]$,他们的电脑每分钟会耗 ...
- Codeforces 1114E - Arithmetic Progression - [二分+随机数]
题目链接:http://codeforces.com/problemset/problem/1114/E 题意: 交互题,有一个 $n$ 个整数的打乱顺序后的等差数列 $a[1 \sim n]$,保证 ...
- Codeforces 660C - Hard Process - [二分+DP]
题目链接:http://codeforces.com/problemset/problem/660/C 题意: 给你一个长度为 $n$ 的 $01$ 串 $a$,记 $f(a)$ 表示其中最长的一段连 ...
随机推荐
- spfa+floyed+最长路+差分约束系统(F - XYZZY POJ - 1932)(题目起这么长感觉有点慌--)
题目链接:https://cn.vjudge.net/contest/276233#problem/F 题目大意:给你n个房子能到达的地方,然后每进入一个房子,会消耗一定的生命值(有可能是负),问你一 ...
- UNIX环境高级编程 第10章 信号
SIGSTOP和SIGKILL区别是:前者是使进程暂时停止,即中止,也就是说使进程暂停,将进程挂起,比如你在终端里面执行一个脚本或者程序,执行到一半,你想暂停一下,你按下ctrl+z,就会导致终端发送 ...
- Jetson TX1 安装Kinect驱动
1.添加Universe源 $ sudo apt-add-repository universe $ sudo apt-get update 2.安装编译工具和依赖项 $ sudo apt-get i ...
- Spiral Matrix I & II
Spiral Matrix I Given an integer n, generate a square matrix filled with elements from 1 to n^2 in s ...
- ARM Linux 3.x的设备树(Device Tree)【转】
转自:http://blog.csdn.net/21cnbao/article/details/8457546 宋宝华 Barry Song <21cnbao@gmail.com> 1. ...
- mvc 分部视图(Partial)显示登陆前后变化以及Shared文件夹在解决方案资源管理器中没有显示的问题
刚开始我的解决方案资源管理器中没有显示Shared文件夹,但Shared文件夹在项目中是实际存在的,我搜了下好像没有类似的解答(可能是我搜索的关键词不够准确).后来自己看了下vs2012. 其实解决方 ...
- wiki confluence安装
注意:安装前请先确认内存 至少2G 1.上传 atlassian-confluence-5.9.3-x64.bin 文件,修改权限 chmod 777 atlassian-confluence-5.9 ...
- 洛谷P3367并查集
传送门 #include <iostream> #include <cstdio> #include <cstring> #include <algorith ...
- thinkphp5与thinkphp3.X对比
原文https://www.cnblogs.com/wupeiky/p/5850108.html 首先声明本章节并非是指导升级旧的项目到5.0,而是为了使用3.X版本的开发者更快的熟悉并上手这个全新的 ...
- SQL SERVER中查询某个表或某个索引是否存在
查询某个表是否存在: 在实际应用中可能需要删除某个表,在删除之前最好先判断一下此表是否存在,以防止返回错误信息.在SQL SERVER中可通过以下语句实现: IF OBJECT_ID(N'表名称', ...