很好的一个自动机的题目。

给原串,和若干个询问串。求原串里有多少个不同子串可以通过询问串循环移动得到。

有点类似求两个串的lcs,但是灵活一点。

首先我们把询问串长度扩大一倍,去掉最后一个字符。因为最后那个字符结尾的情况已经有了。

然后把这个新串拿到SAM中跑一遍,跑的过程就像求lcs差不多,每次判断长度len是否大于询问串长度,以及节点有没有重复加入,来更新答案就好了。

前面自动机的构建以及拓扑排序处理就不说了。

召唤代码君:

#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 4002000
using namespace std; char s[maxn];
int next[maxn][26],pre[maxn],step[maxn],g[maxn],tag[maxn];
int cnt[maxn],Q[maxn];
int N,last,ans,n,L;
int p,q,np,nq; void insert(int x)
{
np=++N,p=last,step[np]=step[p]+1,last=np,g[np]=1;
while (p!=-1 && next[p][x]==0) next[p][x]=np,p=pre[p];
if (p==-1) return;
q=next[p][x];
if (step[q]==step[p]+1) { pre[np]=q; return; }
nq=++N,step[nq]=step[p]+1,pre[nq]=pre[q];
for (int i=0; i<26; i++) next[nq][i]=next[q][i];
pre[np]=pre[q]=nq;
while (p!=-1 && next[p][x]==q) next[p][x]=nq,p=pre[p];
} void process()
{
for (int i=1; i<=N; i++) cnt[step[i]]++;
for (int i=2; i<=N; i++) cnt[i]+=cnt[i-1];
for (int i=1; i<=N; i++) Q[--cnt[step[i]]]=i;
for (int i=N-1; i>=0; i--) g[pre[Q[i]]]+=g[Q[i]];
} int main()
{
pre[0]=-1;
scanf("%s",s);
for (int i=0; s[i]; i++) insert(s[i]-'a');
process();
scanf("%d",&n);
for (int T=1; T<=n; T++)
{
ans=0;
scanf("%s",s+1);
L=strlen(s+1);
for (int i=1; i<L; i++) s[i+L]=s[i];
s[L+L]='\0';
int cur=0,len=0;
for (int i=1; s[i]; i++)
{ int k=s[i]-'a';
while (cur!=-1 && next[cur][k]==0) len=min(len,step[cur]),cur=pre[cur];
if (cur==-1) { cur=0; continue; }
len=min(len,step[cur])+1;
cur=next[cur][k];
for (; step[pre[cur]]>=L; len=min(len,step[cur])) cur=pre[cur];
if (len>=L && tag[cur]!=T) ans+=g[cur],tag[cur]=T;
}
printf("%d\n",ans);
}
return 0;
}

  

CF235C_Cyclical Quest的更多相关文章

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

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

  2. King's Quest —— POJ1904(ZOJ2470)Tarjan缩点

    King's Quest Time Limit: 15000MS Memory Limit: 65536K Case Time Limit: 2000MS Description Once upon ...

  3. cf.VK CUP 2015.C.Name Quest(贪心)

    Name Quest time limit per test 2 seconds memory limit per test 256 megabytes input standard input ou ...

  4. Quest Central for DataBase 5.0.1,6.1 (软件+注册)

    找寻了多天,终于找到了,记录下,以后重装用.输入所有组件的licenses后,提示要注册,我选择了Canada,Google了一个地方的PostCode和phone number,填写,注册成功! 软 ...

  5. codeforces Gym 100500H H. ICPC Quest 水题

    Problem H. ICPC QuestTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100500/a ...

  6. poj 1904 King's Quest

    King's Quest 题意:有N个王子和N个妹子;(1 <= N <= 2000)第i个王子喜欢Ki个妹子:(详见sample)题给一个完美匹配,即每一个王子和喜欢的一个妹子结婚:问每 ...

  7. Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment handle fo

    标题 Error when launching Quest Central for DB2: "QCC10000E - Unable to allocate environment hand ...

  8. 阐述 QUEST CENTRAL FOR DB2 八罪

    作为一个从事oracle plsql发展2猿 - 年计划,现在,在退出DB2数据仓库项目. 同PL/SQL Developer参考,下文PLSQL,阐述QUEST CENTRAL FOR DB2 5. ...

  9. 决策树模型比较:C4.5,CART,CHAID,QUEST

    (1)C4.5算法的特点为: 输入变量(自变量):为分类型变量或连续型变量. 输出变量(目标变量):为分类型变量. 连续变量处理:N等分离散化. 树分枝类型:多分枝. 分裂指标:信息增益比率gain ...

随机推荐

  1. sql server sql查询数据库的表,字段,主键,自增,字段类型等信息

    1.查询数据表的属性(名称.说明.是否主键.数据类型.是否自增) SELECT t1.name columnName,case when t4.id is null then 'false' else ...

  2. c语言数字图像处理(八):噪声模型及均值滤波器

    图像退化/复原过程模型 高斯噪声 PDF(概率密度函数) 生成高斯随机数序列 算法可参考<http://www.doc.ic.ac.uk/~wl/papers/07/csur07dt.pdf&g ...

  3. POJ2251-Dungeon Master(3维BFS)

    You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is composed of un ...

  4. C++ 单例模式总结与剖析

    目录 C++ 单例模式总结与剖析 一.什么是单例 二.C++单例的实现 2.1 基础要点 2.2 C++ 实现单例的几种方式 2.3 单例的模板 三.何时应该使用或者不使用单例 反对单例的理由 参考文 ...

  5. vue 使用ref获取DOM元素和组件引用

    在vue中可以通过ref获取dom元素,并操作它,还可以获取组件里面的数据和方法. HTML部分: <div id="app"> <input type=&quo ...

  6. 理解粒子滤波(particle filter)

    1)初始化阶段-提取跟踪目标特征 该阶段要人工指定跟踪目标,程序计算跟踪目标的特征,比如可以采用目标的颜色特征.具体到Rob Hess的代码,开始时需要人工用鼠标拖动出一个跟踪区域,然后程序自动计算该 ...

  7. [Github] Github使用教程

    前言 Github是一个面向开源及私有软件项目的托管平台.它可以免费使用,并且速度快速,拥有超多的用户.是目前管理软件开发和发现已有代码的首选平台.下面将向Github新手介绍相关操作. 正文 注册 ...

  8. access和MySQL mssql

    Access.MSSQL.MYSQL数据库之间有什么区别?     Access数据库.MSSQL数据库.MYSQL数据库之间有什么区别?        不少企业和个人站长在网站制作时,会对数据库的概 ...

  9. 使用Python批量修改数据库执行Sql文件

    由于上篇文章中批量修改了文件,有的时候数据库也需要批量修改一下,之前的做法是使用宝塔的phpMyAdmin导出一个已经修改好了的sql文件,然后依次去其他数据库里导入,效率不说极低,也算低了,且都是些 ...

  10. (转)Django配置数据库读写分离

    转:https://blog.csdn.net/Ayhan_huang/article/details/78784486 转:http://www.cnblogs.com/dreamer-fish/p ...