#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. python---函数补充(变量传递),语句执行顺序(入栈顺序)

    一:函数补充 默认作为函数参数的数据,是浅拷贝传递.不是和C等语言一样,产生一个临时变量. class T: def __init__(self,num): print(id(num)) self.n ...

  2. Spring 手动提交事务

    在使用Spring声明式事务时,不需要手动的开启事务和关闭事务,但是对于一些场景则需要开发人员手动的提交事务,比如说一个操作中需要处理大量的数据库更改,可以将大量的数据库更改分批的提交,又比如一次事务 ...

  3. C++程序运行时间测定

    From:http://www.cnblogs.com/killerlegend/p/3877703.html Author:KillerLegend Date:2014.7.30 此处程序的测试时间 ...

  4. bzoj千题计划170:bzoj1968: [Ahoi2005]COMMON 约数研究

    http://www.lydsy.com/JudgeOnline/problem.php?id=1968 换个角度 一个数可以成为几个数的约数 #include<cstdio> #incl ...

  5. [IOI2011]Race

    2599: [IOI2011]Race Time Limit: 70 Sec  Memory Limit: 128 MBhttp://www.lydsy.com/JudgeOnline/problem ...

  6. spring框架学习(七)spring管理事务方式之xml配置

    1.DAO AccountDao.java package cn.mf.dao; public interface AccountDao { //加钱 void increaseMoney(Integ ...

  7. js小记 unicode 编码解析

    var str = "\\u6211\\u662Funicode\\u7F16\\u7801"; // 关于这样的数据转换为中文问题,常用的两种方法. // 1. eval 解析 ...

  8. HDU 1181 变形课 (深搜)

    题目连接 Problem Description 呃......变形课上Harry碰到了一点小麻烦,因为他并不像Hermione那样能够记住所有的咒语而随意的将一个棒球变成刺猬什么的,但是他发现了变形 ...

  9. ubuntu16.04 eclipse+pydev 配置

    参考:http://blog.csdn.net/bluish_white/article/details/56509446,http://blog.csdn.net/qing101hua/articl ...

  10. shell脚本实现分日志级别输出

    shell脚本如何优雅的记录日志信息,下面让我们一步一步,让shell脚本的日志也变得高端起来,实现如下功能 ①设定日志级别,实现可以输出不同级别的日志信息,方便调试 ②日志格式类似为:[日志级别] ...