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

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

有点类似求两个串的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. linux分区满了,如何进行扩容

    转自:https://blog.csdn.net/valage/article/details/73332147 图片中可以看到挂载点“/”的利用率移到100%,空间不够,所以要对其进行分区. 1.  ...

  2. Linux(centos7)之更换安装python3(二)

    Linux不比window好安装python,折腾了好久,终于成功安装上了python,window上一个安装包完事,可惜Linux上python版本太低不好使,还要更换为3版本,百度了好久,教程上总 ...

  3. TPO-21 C1 Find a building for orientation

    TPO-21 C1 Find a building for orientation 第 1 段 1.Listen to a conversation between a student and a p ...

  4. 扩展Unity Inspector

    Unity Editor下,可以在不改变原有布局的情况下扩展Inspect的界面. 在继承了Editor的类中,有两种实现方式: using UnityEditor; [CustomEditor(ty ...

  5. 2.5 Oracle之存储过程和MERGE INTO语句

    一.MERGE INTO语句 1.merge into语句的功能:我们操作数据库的时候,有时候会遇到insert或者Update这种需求.我们操纵代码时至少需要写一个插入语句和更新语句并且还得单独写方 ...

  6. HackRF 升级固件到新版本

    本文内容.开发板及配件仅限用于学校或科研院所开展科研实验! 淘宝店铺名称:开源SDR实验室 HackRF链接:https://item.taobao.com/item.htm?spm=a1z10.1- ...

  7. crosstool-ng搭建交叉编译环境注意事项

    一,crosstool-ng的下载及编译方法 可以参考如下网站: http://www.crosstool-ng.org/ 二,编译过程注意事项 1)如果遇到有些代码包不能下载,请依据指定版本,在这里 ...

  8. ifup,ifdown命令详解

    基础命令学习目录首页 原文链接:https://www.cnblogs.com/jing99/p/7881779.html ifup命令网络配置 ifup命令用于激活指定的网络接口.ifdown命令用 ...

  9. 微信小程序开发调试技巧

    1.  查看线上小程序console a.  先打开开发小程序console b.  再打开线上小程序,此时可以查看console

  10. linux安装nginx并配置负载均衡

    linux上安装nginx比较简单: 前提是需要有gcc或者g++ 1.yum需要的依赖  yum -y install openssl openssl-devel 2.解压pcre库.zlib库   ...