Codeforces 235C Cyclical Quest - 后缀自动机
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".
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.
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.
baabaabaaa 5 a ba baa aabaa aaba
7 5 7 3 5
aabbaa 3 aa aabb abba
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 - 后缀自动机的更多相关文章
- CF 235C. Cyclical Quest [后缀自动机]
题意:给一个主串和多个询问串,求询问串的所有样子不同的周期同构出现次数和 没有周期同构很简单就是询问串出现次数,|Right| 有了周期同构,就是所有循环,把询问串复制一遍贴到后面啊!思想和POJ15 ...
- 【Codeforces235C】Cyclical Quest 后缀自动机
C. Cyclical Quest time limit per test:3 seconds memory limit per test:512 megabytes input:standard i ...
- Codeforces 235C. Cyclical Quest
传送门 写的时候挺蛋疼的. 刚开始的时候思路没跑偏,无非就是建个SAM然后把串开两倍然后在SAM上跑完后统计贡献.但是卡在第二个样例上就是没考虑相同的情况. 然后开始乱搞,发现会出现相同串的只有可能是 ...
- CodeForces 235C Cyclical Quest(后缀自动机)
[题目链接] http://codeforces.com/contest/235/problem/C [题目大意] 给出一个字符串,给出一些子串,问每个子串分别在母串中圆环匹配的次数,圆环匹配的意思是 ...
- 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 ...
- Codeforces 235C Cyclical Quest 字符串 SAM KMP
原文链接https://www.cnblogs.com/zhouzhendong/p/CF235C.html 题目传送门 - CF235C 题意 给定一个字符串 $s$ ,多组询问,每组询问的形式为 ...
- Codeforces 452E Three Strings(后缀自动机)
上学期很认真地学了一些字符串的常用工具,各种 suffix structre,但是其实对后缀自动机这个部分是理解地不太透彻的,以致于看了师兄A这题的代码后,我完全看不懂,于是乎重新看回一些学习后缀自动 ...
- Codeforces.700E.Cool Slogans(后缀自动机 线段树合并 DP)
题目链接 \(Description\) 给定一个字符串\(s[1]\).一个字符串序列\(s[\ ]\)满足\(s[i]\)至少在\(s[i-1]\)中出现过两次(\(i\geq 2\)).求最大的 ...
- 后缀自动机(SAM)
*在学习后缀自动机之前需要熟练掌握WA自动机.RE自动机与TLE自动机* 什么是后缀自动机 后缀自动机 Suffix Automaton (SAM) 是一个用 O(n) 的复杂度构造,能够接受一个字符 ...
随机推荐
- 学习笔记<3>View接触
一.View基本概念 1.界面上显示所有的控件都是用对象表示的,即有类,这些类都是View的子类. 2.View的种类 二.在Activity当中获取代表View的对象 1.根据ID可以用方法获取到对 ...
- Python之words count
要求: 对文件单词进行统计,不区分大小写,并显示单词重复最多的十个单词 思路: 利用字典key,value的特性存单词及其重复的次数 每行进行特殊字符的处理,分离出被特殊字符包含的单词 def mak ...
- Keras中使用LSTM层时设置的units参数是什么
https://www.zhihu.com/question/64470274 http://colah.github.io/posts/2015-08-Understanding-LSTMs/ ht ...
- .net 缓存
缓存有很多实现方法,所有这些可以被分为两类,基于内存的缓存和基于磁盘的缓存: 1. 内存驻留缓存——包含在内存中临时存储数据的所有实现方法,通常在以下情况下使用: a) 应用程序频繁使用 ...
- Lua语言总结
[1]要退出交互模式和解释器,只需输入“os.exit()” [2]在交互模式执行程序块可以使用函数dofile,这个函数就可以立即执行一个文件.应用示例:dofile("f:/myLua/ ...
- DSO安装试运行
参考DSO初探 其中Pangolin安装的时候could not find GLEW,参考这里 libx11-dev libxmu-dev libglu1-mesa-dev libgl2ps-dev ...
- redis和mongodb的比较
>>RedisRedis的优点:支持多种数据结构,如 string(字符串). list(双向链表).dict(hash表).set(集合).zset(排序set).hyperloglog ...
- python 某个目录下的所有文件列表
使用os.listdir() 函数来获取某个目录中的文件列表 import os names = os.listdir('somedir') 结果会返回目录中所有文件列表,包括所有文件,子目录,符号链 ...
- linux下安装mysql(rpm文件安装)
数据库包下载: https://www.mysql.com/downloads/ 在GPL开原协议的社区开源版里边下载 我们用mysql community server里边的 其中workbench ...
- python添加新的模块
添加新的模块可以把路径放到环境变量中 或者放到site-packages文件夹下