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. vue中检测敏感词,锚点

    当发布文章的时候,标题有敏感词 则检测有敏感词的接口成功的时候,写锚点 eg: _this .$alert("检测到标题有敏感词,请修改后再发布", "提示", ...

  2. sqli-labs(八)——修改密码处sql注入+http头sql注入

    第17关: 这是一个重置密码的功能存在sqk注入,尝试账号密码都输入'",发现只会显示登陆失败,没有报错信息. 这个时候先推测一下后台的sql形式大概应该是: update users se ...

  3. CSU 1817 Bones’s Battery Submit(二分+Floyd)

    Bones's Battery Submit [题目链接]Bones's Battery Submit [题目类型]二分+Floyd &题意: 有n个点,m条边,从点ui到点vi的费电量是di ...

  4. jQuery-导航下拉菜单-实用简单

    /*CSS代碼*/ /*導航*/ .nav{background: url("../img/menu_bar.gif") repeat-x;} .nav ul li{display ...

  5. kali linux wifi破解(aircrack)

    需要一个能监听的网卡 airmon-ng start wlan0(監聽網卡) airmon-ng check kill(清除其他有影响的環境) airodump-ng mon0 (掃描附近wifi) ...

  6. Linux 压缩解压缩命令详解

    tar -c: 建立压缩档案-x:解压-t:查看内容-r:向压缩归档文件末尾追加文件-u:更新原压缩包中的文件 这五个是独立的命令,压缩解压都要用到其中一个,可以和别的命令连用但只能用其中一个.下面的 ...

  7. HighCharts插件学习(二)

    HighCharts属性 credits: {版权信息} data: {数据功能模块} drilldown: {钻取} exporting: {导出}   labels: {标签}   legend: ...

  8. Spark学习之路 (七)Spark 运行流程

    一.Spark中的基本概念 (1)Application:表示你的应用程序 (2)Driver:表示main()函数,创建SparkContext.由SparkContext负责与ClusterMan ...

  9. CBV源码解析

    1.CBV(class based view) 首先定义一个视图函数,用类的方式定义: 举例: class LoginView(View): def get(self,request): return ...

  10. python 读csv格式的文件

    对于大多数的CSV 格式的数据读写问题,都可以使用csv 库 1. 直接读csv 以下是要操作的csv文件内容 import csv with open(r'C:\Temp\f.csv') as f: ...