原文链接https://www.cnblogs.com/zhouzhendong/p/CF235C.html

题目传送门 -  CF235C

题意

  给定一个字符串 $s$ ,多组询问,每组询问的形式为一个字符串 $T$ ,问 $S$ 有多少个子串与 $T$ 循环同构。(如果 $S$ 有多个相同子串都同构,则算多次)

  $|S|\leq 10^6,\sum |T|\leq 10^6$

题解

  以后坚决不念诗了!中午作死念诗,下午就被一个傻逼错误续了 3 个多钟头。

  做法:

  给 $S$ 建一个 SAM ,然后对于每一个 $T$ KMP 一下,找到最大循环节,在 SAM 上面走一走就可以了。

代码

#include <bits/stdc++.h>
#define right _47hurnf
using namespace std;
typedef long long LL;
const int N=1000005;
int n,m;
char s[N];
struct SAM{
int Next[26],fa,Max;
}t[N<<1];
int tax[N],id[N<<1],right[N<<1];
int size;
void init(){
memset(t,0,sizeof t);
size=1,t[0].Max=-1;
for (int i=0;i<26;i++)
t[0].Next[i]=1;
}
int extend(int p,int c){
if (t[p].Next[c]&&t[p].Max+1==t[t[p].Next[c]].Max)
return t[p].Next[c];
int np=++size,q,nq;
t[np].Max=t[p].Max+1;
for (;!t[p].Next[c];p=t[p].fa)
t[p].Next[c]=np;
q=t[p].Next[c];
if (t[p].Max+1==t[q].Max)
t[np].fa=q;
else {
nq=++size;
t[nq]=t[q],t[nq].Max=t[p].Max+1;
t[q].fa=t[np].fa=nq;
for (;t[p].Next[c]==q;p=t[p].fa)
t[p].Next[c]=nq;
}
return np;
}
int Fail[N];
int KMP(char s[],int n){
Fail[0]=Fail[1]=0;
for (int i=2;i<=n;i++){
int k=Fail[i-1];
while (k>0&&s[i]!=s[k+1])
k=Fail[k];
Fail[i]=k+(s[k+1]==s[i]?1:0);
}
int x=Fail[n];
while (x>0&&n%(n-x)!=0)
x=Fail[x];
return n-x;
}
int main(){
scanf("%s",s+1);
n=strlen(s+1);
init();
for (int i=1,p=1;i<=n;i++)
p=extend(p,s[i]-'a'),right[p]++;
for (int i=1;i<=size;i++)
tax[t[i].Max]++;
for (int i=1;i<=n;i++)
tax[i]+=tax[i-1];
for (int i=1;i<=size;i++)
id[tax[t[i].Max]--]=i;
for (int i=size;i>=1;i--)
right[t[id[i]].fa]+=right[id[i]];
scanf("%d",&m);
while (m--){
scanf("%s",s+1);
int len=strlen(s+1),r=KMP(s,len);
LL ans=0;
int p=1,Matched=0;
for (int i=1;i<=len;i++){
int c=s[i]-'a';
while (!t[p].Next[c])
p=t[p].fa,Matched=t[p].Max;
p=t[p].Next[c];
Matched++;
}
for (int i=1;i<=r;i++){
int c=s[i]-'a';
while (!t[p].Next[c])
p=t[p].fa,Matched=t[p].Max;
p=t[p].Next[c];
Matched++;
while (t[t[p].fa].Max>=len)
p=t[p].fa,Matched=t[p].Max;
if (Matched>=len)
ans+=right[p];
}
printf("%I64d\n",ans);
}
return 0;
}

  

Codeforces 235C Cyclical Quest 字符串 SAM KMP的更多相关文章

  1. Codeforces 235C. Cyclical Quest

    传送门 写的时候挺蛋疼的. 刚开始的时候思路没跑偏,无非就是建个SAM然后把串开两倍然后在SAM上跑完后统计贡献.但是卡在第二个样例上就是没考虑相同的情况. 然后开始乱搞,发现会出现相同串的只有可能是 ...

  2. Codeforces 235C Cyclical Quest - 后缀自动机

    Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in ...

  3. CodeForces 235C Cyclical Quest(后缀自动机)

    [题目链接] http://codeforces.com/contest/235/problem/C [题目大意] 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数,圆环匹配的意思是 ...

  4. Codeforces 316G3 Good Substrings 字符串 SAM

    原文链接http://www.cnblogs.com/zhouzhendong/p/9010851.html 题目传送门 - Codeforces 316G3 题意 给定一个母串$s$,问母串$s$有 ...

  5. Codeforces 452E Three strings 字符串 SAM

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF542E.html 题目传送门 - CF452E 题意 给定三个字符串 $s1,s2,s3$ ,对于所有 $L ...

  6. Codeforces 873F Forbidden Indices 字符串 SAM/(SA+单调栈)

    原文链接https://www.cnblogs.com/zhouzhendong/p/9256033.html 题目传送门 - CF873F 题意 给定长度为 $n$ 的字符串 $s$,以及给定这个字 ...

  7. Codeforces 700E. Cool Slogans 字符串,SAM,线段树合并,动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF700E.html 题解 首先建个SAM. 一个结论:对于parent树上任意一个点x,以及它所代表的子树内任 ...

  8. CF 235C. Cyclical Quest [后缀自动机]

    题意:给一个主串和多个询问串,求询问串的所有样子不同的周期同构出现次数和 没有周期同构很简单就是询问串出现次数,|Right| 有了周期同构,就是所有循环,把询问串复制一遍贴到后面啊!思想和POJ15 ...

  9. Cyclical Quest CodeForces - 235C (后缀自动机)

    Cyclical Quest \[ Time Limit: 3000 ms\quad Memory Limit: 524288 kB \] 题意 给出一个字符串为 \(s\) 串,接下来 \(T\) ...

随机推荐

  1. C++ sizeof()练习

    class A { int a; short b; int c; char d; }; class B { double a; short b; int c; char d; }; 在32位机器上用g ...

  2. List集合三种遍历方法

    List<String> list = new ArrayList<String>();list.add("aaa");list.add("bbb ...

  3. MYSQL修改字段

    当字段为空则插入0,不为空则原来的值  UPDATE t_pm_scheduleSET lesson_room_id1 = IFNULL(lesson_room_id1, 0), lesson_roo ...

  4. Swift 学习- 04 -- 字符串和字符

    // 字符串 和 字符 // 字符串 是有序的 Character (字符) 类型的值的集合,  通过 String 类型的集合 // swift 的 String 和 Character 类型提供了 ...

  5. deepin、Ubuntu安装Nginx

    deepin安装nginx 切换至root用户 su 密码: 基础库的安装 安装gcc g++的依赖库 sudo apt-get install build-essential && ...

  6. mvc 模式和mtc 模式的区别

    首先说说Web服务器开发领域里著名的MVC模式,所谓MVC就是把Web应用分为模型(M),控制器(C)和视图(V)三层,他们之间以一种插件式的.松耦合的方式连接在一起,模型负责业务对象与数据库的映射( ...

  7. CentOS 7 安装 Redis

    1.Redis 下载地址:https://redis.io/download 2.上传到服务器指定文件夹 ,我这边传到了根目录下 /mysoft 这个目录下 解压  tar  -zxvf redis- ...

  8. txt文档去重复内容

    @echo off for /f "delims=" %%i in ('type "%1"') do (if not defined %%i set %%i=A ...

  9. HTML5 CSS3 Transform 笔记 (scale不起作用)

    Transform的 scale属性不能作用于 inline元素上,例如span 并且动画 animation  也不能作用于inline元素上 可以给span加display:inline-bloc ...

  10. Paddington2