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

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

有点类似求两个串的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. [Vue warn]:vue-Failed to resolve directive: clipboard

    前言 需求:移动端需要一个按钮,复制到剪切板,分享给好友(没有调用微信内置的分享接口) 插件 vue-clipboard2 环境:vue,node 安装:npm install --save vue- ...

  2. 用 Python 破解 WIFI 密码,走到哪里都能连 WIFI

    WIFI 破解,Python 程序员必学技能.WIFI 已经完全普及,现在 Python 程序员没网,走到哪里都不怕! 教你们一招,如何在图片中提取 Python 脚本代码.图片发送至手机 QQ 长按 ...

  3. linux 问题收集

    1,错误信息:bunzip2: command not found 解决方法:yum install -y bzip2 2,The X11 forwarding request was rejecte ...

  4. 第一次玩github,第一个开源小项目——xxoo

    引言 由于最近的工作写代码比较少,这让LZ产生了一丝危机感.于是便想找一个办法可以没事自己写写代码,自然而然就想到了github.接下来便是一阵捣鼓的过程,其实整个过程很快,主要过程就是注册一个账号, ...

  5. Cisco Packet Tracer中两台电脑通信设置

    Cisco Packet Tracer是网络初学者仿真模拟网络环境的必备工具.今天我们来模拟下两台电脑之间的通信. Cisco Packet Tracer版本6.2.0 一.添加设备 1.这里添加一个 ...

  6. 使用vs2015编译、部署ssd-caffe(weiliu89版,CPU模式)

    前因项目所需,须训练一个快速模型以实现目标物体的实时检测.历经多次实践,发现MobileNetSSD网络符合要求,故在本人工作PC上部署weiliu89版本的ssd-caffe以期用之训练项目要求之模 ...

  7. AssetBundle压缩/内部结构/下载和加载

    一.AssetBundle的压缩方式   Unity支持三种AssetBundle打包的压缩方式:LZMA, LZ4, 以及不压缩.    1.LZMA压缩方式  是一种默认的压缩形式,这种标准压缩格 ...

  8. Go语言2

    Go语言特点: 类型检查:编译时 运行环境:编译成机器代码直接运行 编程范式:面向接口,函数式编程,并发编程 Go并发编程 采用CSP(Communication Sequenication Proc ...

  9. Vue03

    5.组件化开发 组件[component] 在网页中实现一个功能,需要使用html定义功能的内容结构,使用css声明功能的外观样式,还要使用js来定义功能的特效,因此就产生了把一个功能相关的[HTML ...

  10. Inception——Going deeper with convolutions

    1. 摘要 作者提出了一个代号为 Inception 的卷积神经网络架构,这也是作者在 2014 年 ImageNet 大规模视觉识别挑战赛中用于分类和检测的新技术. 通过精心的设计,该架构提高了网络 ...