#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. openstack虚拟机启动过程源码分析

    源码版本:H版 以nova-api为起点开始分析! 一.在nova-api进程中进行处理 根据对nova api的分析,当请求发过来的时候,由相应的Controller进行处理,此处如下: nova/ ...

  2. UITextView默认文字提示

    在UITextField中自带placeholder属性,可以用于提示输入框信息.但是UITextView并不具备此功能介绍两种方法来实现:第一种:初始化UITextView//首先定义UITextV ...

  3. bzoj千题计划160:bzoj2599: [IOI2011]Race

    http://www.lydsy.com/JudgeOnline/problem.php?id=2599 点分治 mi[i] 记录边权和为i时的最少边数 先更新答案,再更新mi数组,换根时清空mi # ...

  4. Docker查看映射卷报错

    问题描述: 当查看Docker容器的映射卷时出现报错信息,如下: [root@kazihuo ~]# docker inspect -f {{.Volumes}} volume   #volume指容 ...

  5. JavaScript实现单向链表

    JavaScript 本身提供了十分好用的数据类型,以满足大家的日常使用.单靠 Array  和 Object 也的确足够应付日常的绝大部分需求,这也导致了很多前端er对数据结构这一块不是十分的了解. ...

  6. window环境下使用sbt编译spark源码

    前些天用maven编译打包spark,搞得焦头烂额的,各种错误,层出不穷,想想也是醉了,于是乎,换种方式,使用sbt编译,看看人品如何! 首先,从官网spark官网下载spark源码包,解压出来.我这 ...

  7. 2016.07.13-map的使用(以leetcode1-Two Sum为例)

    map的使用 1.unordered_map和map的区别 2.如何用 3.for (int a : nums1) 4.to_string() 5.map的应用 1.unordered_map和map ...

  8. Spring4笔记11--SSH整合2--SpringWeb

    SSH 框架整合技术: 2. Spring 在 Web 项目中的使用(建立在Spring与Hibernate整合的基础上): 在 Web 项目中使用 Spring 框架,首先要解决在 Servlet ...

  9. Spring4笔记6--Spring与AOP

    Spring与AOP: AOP的引入: 主业务经常需要调用系统级业务(交叉业务),如果在主业务代码中大量的调用系统级业务代码,会使系统级业务与主业务深度耦合在一起,大大影响了主业务逻辑的可读性,降低了 ...

  10. centos 安装memcache服务后memcahce本机连接Permission

    自己手动在虚拟机下装了下memcache,整个过程真是充满波折,本身用php5.3安装memcache扩展就麻烦很多,无法通过yum直接安装,安装方法详见http://chenwei.me/blog/ ...