Some days ago, WJMZBMR learned how to answer the query "how many times does a string x occur in a string s" quickly by preprocessing the string s. But now he wants to make it harder.

So he wants to ask "how many consecutive substrings of s are cyclical isomorphic to a given string x". You are given string s and n strings xi, for each string xi find, how many consecutive substrings of s are cyclical isomorphic to xi.

Two strings are called cyclical isomorphic if one can rotate one string to get the other one. 'Rotate' here means 'to take some consecutive chars (maybe none) from the beginning of a string and put them back at the end of the string in the same order'. For example, string "abcde" can be rotated to string "deabc". We can take characters "abc" from the beginning and put them at the end of "de".

Input

The first line contains a non-empty string s. The length of string s is not greater than 106 characters.

The second line contains an integer n (1 ≤ n ≤ 105) — the number of queries. Then n lines follow: the i-th line contains the string xi — the string for the i-th query. The total length of xi is less than or equal to 106 characters.

In this problem, strings only consist of lowercase English letters.

Output

For each query xi print a single integer that shows how many consecutive substrings of s are cyclical isomorphic to xi. Print the answers to the queries in the order they are given in the input.

Examples
Input
baabaabaaa 5 a ba baa aabaa aaba
Output
7 5 7 3 5
Input
aabbaa 3 aa aabb abba
Output
2 3 3

  题目大意 给定一个字符串s,和一堆字符串x,问每个字符串x和多少个s的子串循环同构。字符串s和字符串t循环同构是指,将s的某个前缀(可以是空串)挪到s的尾部使得和字符串t相等。

  显然后缀自动机

  先对s建立后缀自动机。然后对于每个不是为了解决分割状态的状态的cnt设为1,接着从parent树的叶节点开始向上累加cnt。

  对于每个询问的x,我们将x的除去最后一个字符的串复制一份,塞进x的末尾。然后扔进s的后缀自动机匹配LCS,当发现当前匹配的长度大于等于串x原本的长度,然后就开始跳par指针(但是我比较喜欢写fail),直到当前匹配的长度为串x原本长度的子串的长度存在当前状态的长度区间内,然后累计答案。

  由于这么做会重复一些东西,比如输入的串像什么"aaaaaaaa",显然就会重复计数。所以你需要给每个节点记录一个时间戳来判断是否被当前询问统计过。

我最开始的想法是,对于两个循环同构的串,显然可以保留其中某一个的后缀然后把剩下的部分挪到后面去使得两个串相等。

后缀自动机中跳par指针,实质上是正在访问某个子串的后缀。

所以考虑如果len - 1不在当前的长度区间内,就往回跳一步去匹配下一个字符(指前面的字符转上来),假设直接到达下一个字符,然后累加答案,更新时间戳。

如果过程不是很顺利,发生了失配,那么又变成了一个当前串的后缀,于是,需要把舍弃的部分加上来,然后继续去for。

简单地说就是设法线性地实现循环同构的匹配。

  (后来我想了想,发现这两种做法本质是相同的。)

Code

 /**
* Codeforces
* Problem#235C
* Accepted
* Time:514ms
* Memory:278100k
*/
#include <bits/stdc++.h>
using namespace std; #define charset 26
//这是一个膜拜大佬的宏定义
#define ModYJQ 2333333 inline int cti(char x) { return x - 'a'; } typedef class TrieNode {
public:
TrieNode* next[charset];
TrieNode* fail;
vector<TrieNode*> son;
int cnt;
int len;
int t;
TrieNode():fail(NULL), cnt(), len(), t(ModYJQ) {
memset(next, , sizeof(next));
}
}TrieNode; typedef class SuffixAutomachine {
public:
TrieNode *pool;
TrieNode *top; TrieNode* root;
TrieNode* last; TrieNode* newnode() {
return top++;
} TrieNode* newnode(int len) {
top->len = len;
return top++;
} SuffixAutomachine():pool(NULL), top(NULL), root(NULL), last(NULL) { }
SuffixAutomachine(int len) {
pool = new TrieNode[len * + ];
top = pool;
root = newnode();
last = root;
} inline void extend(char x) {
int c = cti(x);
TrieNode* node = newnode(last->len + ), *f = last;
node->cnt = ;
while(f && f->next[c] == NULL)
f->next[c] = node, f = f->fail;
if(f == NULL) node->fail = root;
else {
TrieNode *p = f->next[c];
if(p->len == f->len + ) node->fail = p;
else {
TrieNode *clone = newnode(f->len + );
memcpy(clone->next, p->next, sizeof(clone->next));
clone->fail = p->fail;
p->fail = clone;
node->fail = clone;
while(f && f->next[c] == p)
f->next[c] = clone, f = f->fail;
}
}
last = last->next[c];
}
}SuffixAutomachine; int lens;
int n;
char str[];
SuffixAutomachine sam; inline void init() {
gets(str);
lens = strlen(str);
} int dfs(TrieNode* p) {
for(int i = ; i < (signed)p->son.size(); i++)
p->cnt += dfs(p->son[i]);
return p->cnt;
} inline void init_sam() {
sam = SuffixAutomachine(lens);
for(int i = ; i < lens; i++)
sam.extend(str[i]);
for(TrieNode* p = sam.pool; p != sam.top; p++) {
if(p->fail)
p->fail->son.push_back(p);
}
dfs(sam.root);
} inline void solve() {
scanf("%d", &n);
gets(str);
while(n--) {
gets(str);
lens = strlen(str);
memcpy(str + lens, str, sizeof(char) * (lens - ));
TrieNode* p = sam.root, *q;
int res = ;
for(int i = , nlen = , c; i < * lens - && p; i++) {
c = cti(str[i]);
if(!p->next[c]) {
while(p && !p->next[c]) p = p->fail, nlen = (p) ? (p->len) : ();
if(!p) break;
}
p = p->next[c], nlen++;
// printf("%d %d\n", lens, nlen);
if(nlen >= lens) {
q = p;
while(q->fail->len >= lens)
q = q->fail;
if(q->t != n) {
res += q->cnt;
q->t = n;
}
}
}
printf("%d\n", res);
}
} int main() {
init();
init_sam();
solve();
return ;
}

Codeforces 235C Cyclical Quest - 后缀自动机的更多相关文章

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

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

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

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

  3. Codeforces 235C. Cyclical Quest

    传送门 写的时候挺蛋疼的. 刚开始的时候思路没跑偏,无非就是建个SAM然后把串开两倍然后在SAM上跑完后统计贡献.但是卡在第二个样例上就是没考虑相同的情况. 然后开始乱搞,发现会出现相同串的只有可能是 ...

  4. CodeForces 235C Cyclical Quest(后缀自动机)

    [题目链接] http://codeforces.com/contest/235/problem/C [题目大意] 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数,圆环匹配的意思是 ...

  5. Codeforces Round #146 (Div. 1) C - Cyclical Quest 后缀自动机+最小循环节

    #include<bits/stdc++.h> #define LL long long #define fi first #define se second #define mk mak ...

  6. Codeforces 235C Cyclical Quest 字符串 SAM KMP

    原文链接https://www.cnblogs.com/zhouzhendong/p/CF235C.html 题目传送门 -  CF235C 题意 给定一个字符串 $s$ ,多组询问,每组询问的形式为 ...

  7. Codeforces 452E Three Strings(后缀自动机)

    上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...

  8. Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)

    题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...

  9. 后缀自动机(SAM)

    *在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...

随机推荐

  1. (已解决)Eclipse报错:Could not find XXX.apk. 没有Android项目命名. There is no android project named

    可能是你把当前项目设置为library项目了,按以下步骤切换回普通项目: 选择 Project->Properties 在左边的列表中,选择 Android 取消钩中"Is Libra ...

  2. OEMCC13.2 添加监控目标

    1.需求描述 2.添加数据库目标 2.1 部署AGENT   2.1.1 直接安装方式   2.1.2 离线安装方式   2.1.3 命令行安装方式 2.2 添加集群资源 2.3 添加数据库 3.添加 ...

  3. 大数据-05-Spark之读写HBase数据

    本文主要来自于 http://dblab.xmu.edu.cn/blog/1316-2/ 谢谢原作者 准备工作一:创建一个HBase表 这里依然是以student表为例进行演示.这里假设你已经成功安装 ...

  4. System.BadImageFormatException”C#报错

    在平常的开发中或多或少会遇到一些问题,而本次向小编这里是自己刚刚解决的一个问题,贴出来与大家分享一下,纠结了一个下午,终于解决了,是有关平台的一个报错问题.   方法/步骤     报错”“Syste ...

  5. input 滑块功能range javascript方法使用

    <script> var rangelist=document.querySelectorAll('[type="range"]'); for(var i=0; i&l ...

  6. 删除SQL Server大容量日志的方法(转)

    删除SQL Server大容量日志的方法 亲自实践的方法 1.分享数据库,如果提示被其他连接占用,不能分离,刚勾上drop connections 2.复制下所有文件,一定要备份好,以防自己操作失误 ...

  7. SV processses

    SV中的structured procedure: 1)intial procedure,keyword只有initial:最开始被调用一次: 2)always procedure,keyword包括 ...

  8. 54. Spiral Matrix(剑指offer 19)

    Given a matrix of m x n elements (m rows, n columns), return all elements of the matrix in spiral or ...

  9. jQuery选择器--:selected和:checked

    :selected 概述 匹配所有选中的option元素 <!DOCTYPE html> <html> <head> <meta charset=" ...

  10. 【Hbase学习之一】Hbase 简介

    环境 虚拟机:VMware 10 Linux版本:CentOS-6.5-x86_64 客户端:Xshell4 FTP:Xftp4 jdk8 hadoop-3.1.1 apache-hive-2.1.3 ...