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

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

有点类似求两个串的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. Altium 中PCB的Gerber生产资料的输出详细步骤

    生产文件的输出,俗称Gerber out,Gerber文件是所有电路设计软件都可以产生的文件,在电子组装行业又称为模版文件(Stencil Data),在PCB制造业又称为光绘文件.可以说Gerber ...

  2. 转载Linux下开启MySQL日志

    转载https://blog.csdn.net/weixin_38187469/article/details/79273962 开启mysql日志   1.查看日志是否启用 mysql> sh ...

  3. c语言数字图像处理(六):二维离散傅里叶变换

    基础知识 复数表示 C = R + jI 极坐标:C = |C|(cosθ + jsinθ) 欧拉公式:C = |C|ejθ 有关更多的时域与复频域的知识可以学习复变函数与积分变换,本篇文章只给出DF ...

  4. 关于UC浏览器兼容scroll事件问题

    经过本人查阅无数资料,最终得出一个比较简单,具有一定兼容性的结果. $(window).scroll(function( ) { var scrollTop = document.documentEl ...

  5. HTML基础范例

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 10分钟了解 pandas - pandas官方文档译文 [原创]

    10 Minutes to pandas 英文原文:https://pandas.pydata.org/pandas-docs/stable/10min.html 版本:pandas 0.23.4 采 ...

  7. webpack2.0+ vue2.0

    一 webpack 2.0 及用到的插件安装(默认已经有node环境) 1. package.json文件 (插件安装及插件的功能不详解) { "private": true, & ...

  8. k8s环境搭建--基于kubeadm方法

    环境 master node: 数量 1, 系统 ubuntu 16.04_amd64 worker node: 数量 1, 系统 ubuntu 16.04_amd64 kubernetes 版本: ...

  9. 51nod-1298 圆与三角形(计算几何超详解)

    题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1298 给出圆的圆心和半径,以及三角形的三个顶点,问圆同三角形是 ...

  10. git ssh密钥配置添加

    1.  初次安装git配置用户名和邮箱 $ git config --global user.name "xxx" $ git config --global user.email ...