#include<bits/stdc++.h>
#define LL long long
#define fi first
#define se second
#define mk make_pair
#define PII pair<int, int>
#define PLI pair<LL, int>
#define ull unsigned long long
using namespace std; const int N = 2e6 + ;
const int inf = 0x3f3f3f3f;
const LL INF = 0x3f3f3f3f3f3f3f3f;
const int mod = 1e9 + ;
const double eps = 1e-; int n, q, nx[N];
char s[N]; void getNext(char *s, int n) {
int k = ;
for(int i = ; i < n; i++) {
while(k && s[k] != s[i]) k = nx[k-];
if(s[k] == s[i]) k++;
nx[i] = k;
}
} struct SuffixAutomaton {
int last, cur, cnt, ch[N<<][], id[N<<], fa[N<<], dis[N<<], sz[N<<], c[N];
SuffixAutomaton() {cur = cnt = ;}
void init() {
for(int i = ; i <= cnt; i++) {
memset(ch[i], , sizeof(ch[i]));
sz[i] = c[i] = dis[i] = fa[i] = ;
}
cur = cnt = ;
}
int extend(int p, int c) {
cur = ++cnt; dis[cur] = dis[p]+;
for(; p && !ch[p][c]; p = fa[p]) ch[p][c] = cur;
if(!p) fa[cur] = ;
else {
int q = ch[p][c];
if(dis[q] == dis[p]+) fa[cur] = q;
else {
int nt = ++cnt; dis[nt] = dis[p]+;
memcpy(ch[nt], ch[q], sizeof(ch[q]));
fa[nt] = fa[q]; fa[q] = fa[cur] = nt;
for(; ch[p][c]==q; p=fa[p]) ch[p][c] = nt;
}
}
sz[cur] = ;
return cur;
}
void getSize(int n) {
for(int i = ; i <= cnt; i++) c[dis[i]]++;
for(int i = ; i <= n; i++) c[i] += c[i-];
for(int i = cnt; i >= ; i--) id[c[dis[i]]--] = i;
for(int i = cnt; i >= ; i--) sz[fa[id[i]]] += sz[id[i]];
} void solve() {
scanf("%s", s + );
n = strlen(s + );
for(int i = , last = ; i <= n; i++)
last = extend(last, s[i]-'a');
getSize(n);
scanf("%d", &q);
while(q--) {
scanf("%s", s + );
n = strlen(s + );
int tar = n;
getNext(s + , n);
int len = n%(n-nx[n-]) ? n : (n-nx[n-]);
for(int i = ; i < len; i++) s[++n] = s[i];
len = ;
LL ans = ;
for(int i = , p = ; i <= n; i++) {
while(p!= && !ch[p][s[i]-'a']) p = fa[p];
if(ch[p][s[i]-'a']) {
len = min(dis[p], len) + ;
p = ch[p][s[i]-'a'];
}
else len = ;
if(len >= tar) {
int u = p;
while(u != && dis[fa[u]] >= tar) u = fa[u];
if(u != ) ans += sz[u];
}
}
printf("%lld\n", ans);
}
}
} sam; int main() {
sam.solve();
return ;
} /*
*/

Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节的更多相关文章

  1. Codeforces Round #117 (Div. 2) D.Common Divisors(KMP最小循环节)

    http://codeforces.com/problemset/problem/182/D 题意:如果把字符串a重复m次可以得到字符串b,那么我们称字符串a为字符串b的一个因子,现在给定两个字符串S ...

  2. codeforces 825F F. String Compression dp+kmp找字符串的最小循环节

    /** 题目:F. String Compression 链接:http://codeforces.com/problemset/problem/825/F 题意:压缩字符串后求最小长度. 思路: d ...

  3. Codeforces Round #146 (Div. 2)

    A. Boy or Girl 模拟题意. B. Easy Number Challenge 筛素数,预处理出\(d_i\). 三重循环枚举. C. LCM Challenge 打表找规律. 若\(n\ ...

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

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

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

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

  6. Codeforces Round #146 (Div. 1) B. Let's Play Osu! dp

    B. Let's Play Osu! 题目连接: http://www.codeforces.com/contest/235/problem/B Description You're playing ...

  7. Codeforces Round #146 (Div. 1) A. LCM Challenge 水题

    A. LCM Challenge 题目连接: http://www.codeforces.com/contest/235/problem/A Description Some days ago, I ...

  8. Codeforces Round #248 (Div. 1) D - Nanami's Power Plant 最小割

    D - Nanami's Power Plant 思路:类似与bzoj切糕那道题的模型.. #include<bits/stdc++.h> #define LL long long #de ...

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

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

随机推荐

  1. java反射机制的理解

    反射机制是什么概念?大多都有介绍,指的是程序在运行状态中,能够加载一个只有类名的类,加载完之后会在堆上产生一个Class对象.通过这个 Class对象可以获得类的属性.方法和其他类信息.之前对反射的应 ...

  2. openGL笔记-画基本图形

    #include "iostream" #include <GL/glut.h> #include<cmath> #include<vector> ...

  3. [整理]C结构实现位段(bit field)

    #include <stdio.h> #include <string.h> typedef struct A{ int a:5; int b:3; unsigned c:8; ...

  4. python核心编程笔记——Chapter5

    好吧,开始第五章习题: 5-2.又是老调重谈,raw_input这个函数真是非常麻烦,返回是str类型,没办法,只能在函数里面使用int内建.(就只是一道简单的两数相乘,哭了) #!/usr/bin/ ...

  5. 精心整理的十个必须要知道CSS+DIV技巧

    1.css font的简写规则  当我们写字体样式的时候,我们也许会这样子写 font-size: 1em; line-height: 1.5em; font-weight: bold; font-s ...

  6. Go学习中

    教程 http://www.runoob.com/go/go-slice.html Go语言中的管道(Channel)总结 http://www.cnblogs.com/yetuweiba/p/436 ...

  7. python类和对象-扩展

    1.为类或对象动态创建属性或方法 2.__slots__ = ('name','age'),[实例]只能创建指定的属性属性或方法 ---新式类 3.执行父类的构造函数的不同Father.__init_ ...

  8. php中路径斜杠的应用,兼容win与linux

    更多内容推荐微信公众号,欢迎关注: PHP中斜杠的运用 兼容win和linux 使用常量:DIRECTORY_SEPARATOR如:"www".DIRECTORY_SEPARATO ...

  9. Kissy && Require

    KISSY add(name?,factory?,deps)  函数挂载在全局对象KISSY上,用来定义模块.   一个 JS 文件包含一个add()(这时路径+文件名可以用作模块名),如果一个文件包 ...

  10. Spring笔记13--SSH--全注解开发

    SSH全注解开发: (1) 在Action类中添加注解,实现Struts2的注解开发(@NameSpace.@ParentPackage.@Action...) package com.tongji. ...