题意

给出一个字符串s1和q个询问,每个询问给出一个字符串s2,问这个询问的字符串的所有不同的周期串在s1中出现的次数的和。

分析

对于s1建后缀自动机。对于询问的每个字符串s2,我们按照处理循环串的方法,将它长度乘二再复制一遍。然后根据s2在自动机上跑,当长度len=n的时候,就更新答案。因为要求统计的是不同的周期串,所以对于每个状态都需要打一个vis标记。

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std;
const int maxn=2e6+;
char s[maxn];
struct state{
int len,link;
int next[];
}st[*maxn];
int last,cur,sz,Q,n;
int cnt[*maxn],c[*maxn],vis[*maxn];
void init(){
sz=;
last=cur=;
st[].link=-;
st[].len=;
}
void build_sam(int c){
cur=sz++;
st[cur].len=st[last].len+;
cnt[cur]=;
int p;
for(p=last;p!=-&&st[p].next[c]==;p=st[p].link){
st[p].next[c]=cur;
}
if(p==-)
st[cur].link=;
else{
int q=st[p].next[c];
if(st[q].len==st[p].len+)
st[cur].link=q;
else{
int clone=sz++;
st[clone].len=st[p].len+;
st[clone].link=st[q].link;
for(int i=;i<;i++)
st[clone].next[i]=st[q].next[i];
for(;p!=-&&st[p].next[c]==q;p=st[p].link){
st[p].next[c]=clone;
}
st[cur].link=st[q].link=clone;
}
}
last=cur;
}
int cmp(int a,int b){
return st[a].len>st[b].len;
}
int solve(int id){
int res=;
int u=,len=;
for(int i=;i<*n-;i++){
int c=s[i]-'a';
while(u!=-&&(st[u].next[c]==))
u=st[u].link,len=st[u].len;
if(u==-)
u=,len=;
else{
u=st[u].next[c];
len++;
if(len>=n&&vis[u]!=id){
res+=cnt[u];
vis[u]=id;
}
while(n!=&&st[u].link!=-&&st[st[u].link].len>=n-)
u=st[u].link,len=st[u].len;
}
}
return res;
} int main(){
scanf("%s",s);
n=strlen(s);
init();
for(int i=;i<n;i++)
build_sam(s[i]-'a');
for(int i=;i<sz;i++)
c[i]=i;
sort(c,c+sz,cmp);
for(int i=;i<sz;i++){
int o=c[i];
if(st[o].link!=-){
cnt[st[o].link]+=cnt[o];
}
} scanf("%d",&Q);
for(int i=;i<=Q;i++){
// memset(vis,0,sizeof(vis));
scanf("%s",s);
n=strlen(s);
for(int j=;j<n;j++)
s[j+n]=s[j];
int res=solve(i);
printf("%d\n",res);
}
return ;
}

【CodeForces - 235C】Cyclical Quest 【后缀自动机】的更多相关文章

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

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

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

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

  3. 【Codeforces235C】Cyclical Quest 后缀自动机

    C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...

  4. Codeforces 235C. Cyclical Quest

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

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

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

  6. Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  7. Codeforces 235C Cyclical Quest 字符串 SAM KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF235C.html 题目传送门 -  CF235C 题意 给定一个字符串 $s$ ,多组询问,每组询问的形式为 ...

  8. Codeforces 452E Three Strings(后缀自动机)

    上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...

  9. Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)

    题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...

  10. 后缀自动机(SAM)

    *在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...

随机推荐

  1. 使用 openresty 修改请求内容

    1. 目的    动态修改 html 页面内容   2. 使用方式    openresty  在 header_filter 阶段 以及body_filter 阶段进行数据修改   3. 源码  此 ...

  2. angular-ui-bootstrap弹出框的使用(一)

    在开发项目时,我们经常性的会遇到弹出框的需求,例如登陆,注册,添加信息等等....面对这一需求,我们当然也可以使用自己的双手进行编写,如果你时间充足可以试试. 今天我们讲解一下如何在angular框架 ...

  3. erlang的调试配置

    distel可以断点调试erlang,但是步骤非常繁琐 以下面的测试脚本为例说明 geometry.erl -module(geometry). -export([area/1]). area({re ...

  4. idea之jrebel热部署使用教程

    JRebel是一个J2EE热部署的工具.使用它可以减少浪费8-18%的开发时间在项目的构建和部署上.虽然Java也提供了HotSpot的JVM,但是如果你修改的类中有方法名称变动的话,HotSpot就 ...

  5. POJ 1222 EXTENDED LIGHTS OUT(反转)

    EXTENDED LIGHTS OUT Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 12616   Accepted: 8 ...

  6. Tool:Visual Studio Code

    ylbtech-Tool:Visual Studio Code Microsoft在2015年4月30日Build 开发者大会上正式宣布了 Visual Studio Code 项目:一个运行于 Ma ...

  7. Warning: require(D:\wamp\www\glink-smart\bootstrap/../vendor/autoload.php): failed to open stream: No such file or directory in D:\wamp\www\glink-smart\bootstrap\autoload.php on line 1

    Laravel访问出错错误信息:`Warning: require(/vendor/autoload.php): failed to open stream: No such file or dire ...

  8. hadoop集群调优-hadoop settings and MapReduce

    Hadoop Settings 由于Hadoop节点的系统配置,一些hadoop的设置可以减少运行系统中的瓶颈.首先,提高Java运行时的堆内存容量,也要和系统中的整体内存容量相关:其次,保持hado ...

  9. Windows2012使用笔记

    一.介绍 win 2012的名字于北京时间2012年4月18日公布,全称Windows Server 2012(下面简称win 2012),正式版于2012年9月4日发布.这是一套基于Windows ...

  10. msyqld 的 The user specified as a definer ('root'@'%') does not exist 问题

    msyqld 的 The user specified as a definer ('root'@'%') does not exist 问题 造成问题:搭建网站时显示内容不完整. 跟踪tomcat日 ...