cf 235C 后缀自动机
题目大意
给定字符串\(S\)与\(n<=10^5\)个串\(x_1,x_2...x_n\)(总长\(\le10^6\))
对于每个\(x_i\),输出有多少个\(S\)的子串与\(x_i\)循环流动
分析
对\(S\)建自动机
对于每个\(x\)将\(xx\)放进去匹配
暴力的做法是像\(LCS_2\)那样跑完后,按拓扑序遍历更新一次自动机
显然\(TLE\)
\(len\)为当前匹配长度
于是我们每匹配到一个\(len>=|x|\)就去算这个\(|x|\)长度子串出现次数
要往父亲跳直到\(min(p)<=|x|<=max(p)\)
跳完后不能回到原位置继续匹配,这样复杂度有问题
跳完后可以直接\(len=max(或|x|)\)不会影响答案
solution
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <algorithm>
using namespace std;
const int M=2000007;
char s[M];
int n,m;
int last,tot;
int ch[M][26];
int stp[M],fa[M];
int right[M];
int pos[M],sum[M];
int vis[M],T;
int newnode(int ss){
stp[++tot]=ss;
return tot;
}
int ext(int p,int q,int d){
int nq=newnode(stp[p]+1);
fa[nq]=fa[q]; fa[q]=nq;
memcpy(ch[nq],ch[q],sizeof(ch[q]));
for(;p&&ch[p][d]==q;p=fa[p]) ch[p][d]=nq;
return nq;
}
int sam(int p,int d){
int np=ch[p][d];
if(np) return (stp[p]+1==stp[np]) ? np : ext(p,np,d);
np=newnode(stp[p]+1);
for(;p&&!ch[p][d];p=fa[p]) ch[p][d]=np;
if(!p) fa[np]=1;
else{
int q=ch[p][d];
fa[np]= (stp[p]+1==stp[q]) ? q : ext(p,q,d);
}
return np;
}
void match(){
int p=1,len=0,i,d;
int res=0;
n=strlen(s+1);
for(i=1;i<=n;i++) s[i+n]=s[i];
for(i=1;i<=n*2;i++){
d=s[i]-'a';
if(!ch[p][d]){
for(;p&&!ch[p][d];p=fa[p]);
if(!p) p=1,len=0;
else{
len=stp[p]+1;
p=ch[p][d];
}
}
else{
len++;
p=ch[p][d];
}
if(len>=n){
for(;n<=stp[fa[p]];p=fa[p]);
len=stp[p];
if(vis[p]!=T){
vis[p]=T;
res+=right[p];
}
}
}
printf("%d\n",res);
}
int main(){
int i;
scanf("%s",s+1);
n=strlen(s+1);
last=tot=1;
for(i=1;i<=n;i++){
last=sam(last,s[i]-'a');
right[last]=1;
}
for(i=1;i<=tot;i++) sum[stp[i]]++;
for(i=1;i<=tot;i++) sum[i]+=sum[i-1];
for(i=1;i<=tot;i++) pos[sum[stp[i]]--]=i;
for(i=tot;i>1;i--) right[fa[pos[i]]]+=right[pos[i]];
scanf("%d",&m);
while(m--){
scanf("%s",s+1);
T++;
match();
}
return 0;
}
cf 235C 后缀自动机的更多相关文章
- Cyclical Quest CodeForces - 235C 后缀自动机
题意: 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数, 圆环匹配的意思是将该子串拆成两段再首位交换相接的串和母串匹配,比 如aaab变成baaa,abaa,aaba再进行匹配. ...
- Codeforces 235C Cyclical Quest - 后缀自动机
Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...
- CF 666E Forensic Examination——广义后缀自动机+线段树合并
题目:http://codeforces.com/contest/666/problem/E 对模式串建广义后缀自动机,询问的时候把询问子串对应到广义后缀自动机的节点上,就处理了“区间”询问. 还要处 ...
- CF 316G3 Good Substrings——广义后缀自动机
题目:http://codeforces.com/contest/316/problem/G3 对询问串和模式串一起建一个后缀自动机,做出在每个串上的 right 集合大小之后枚举自动机上的每个点看看 ...
- 【CodeForces - 235C】Cyclical Quest 【后缀自动机】
题意 给出一个字符串s1和q个询问,每个询问给出一个字符串s2,问这个询问的字符串的所有不同的周期串在s1中出现的次数的和. 分析 对于s1建后缀自动机.对于询问的每个字符串s2,我们按照处理循环串的 ...
- CF G. Indie Album 广义后缀自动机+树链剖分+线段树合并
这里给出一个后缀自动机的做法. 假设每次询问 $t$ 在所有 $s$ 中的出现次数,那么这是非常简单的: 直接对 $s$ 构建后缀自动机,随便维护一下 $endpos$ 大小就可以. 然而,想求 $t ...
- CF 149E Martian Strings 后缀自动机
这里给出来一个后缀自动机的题解. 考虑对 $s$ 的正串和反串分别建后缀自动机. 对于正串的每个节点维护 $endpos$ 的最小值. 对于反串的每个节点维护 $endpos$ 的最大值. 这两个东西 ...
- 后缀自动机(SAM)
*在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...
- 【算法】后缀自动机(SAM) 例题
算法介绍见:http://www.cnblogs.com/Sakits/p/8232402.html 广义SAM资料:https://www.cnblogs.com/phile/p/4511571.h ...
随机推荐
- 第四篇、Swift_Podfile文件配置格式
# Uncomment this line to define a global platform for your project platform :ios, '9.0' # Comment th ...
- macbook pro开机键盘键盘和触摸板没反应问题
今天遇到开机键盘和触摸板没反应的问题,打电话给售后,他叫我插一个usb外置键盘,开机时按shift+alt+control+电源键开机,突然发现可以了,这bug我也是醉了
- 洛谷P1049装箱问题
一句话刚刚的题会了,这题能不会么. #include<bits/stdc++.h> using namespace std; int main(){ int n,m; cin>> ...
- Date.prototype.Format---对Date的扩展
// 对Date的扩展,将 Date 转化为指定格式的String // 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占 ...
- linux正则表达式基础部分
1.什么是正则表达式? 简单的说,正则表达式就是为处理大量的字符串而定义的一套规则和方法, 例如:假设“@”代表boy,“!”代表girl.echo“@!” === “boygirl” 通过定义的这些 ...
- PHP PDO 使用类
PDO类 <?php class MYPDO { protected static $_instance = null; protected $dbName = ''; protected $d ...
- mysql 5.7初始化默认密码错误
下载了一个mysql 5.7.17的安装包后,安装后怎么都启动不了,好在mysql安装是成功了,没办法只有使用命令行重新初始化设置了 我的mysql安装根目录为:C:\Program Files\My ...
- 分享 php array_column 函数 无法在低版本支持的 修改
function i_array_column($input, $columnKey, $indexKey=null){ if(!function_exists('array_column')){ $ ...
- python 3 在工作中的应用
Python 3在工作中的使用 安装配置Python 3 在notepad++中配置Python 3 使用sql server数据库 操作Excel 发送email python 3 使用日志 安 ...
- 并查集:HDU1213-How Many Tables(并查集最简单的应用)
How Many Tables Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tot ...