HDU4787 GRE Words Revenge(AC自动机 分块 合并)
题目
Source
http://acm.hdu.edu.cn/showproblem.php?pid=4787
Description
Now Coach Pang is preparing for the Graduate Record Examinations as George did in 2011. At each day, Coach Pang can:
"+w": learn a word w
"?p": read a paragraph p, and count the number of learnt words. Formally speaking, count the number of substrings of p which is a learnt words.
Given the records of N days, help Coach Pang to find the count. For convenience, the characters occured in the words and paragraphs are only '0' and '1'.
Input
The first line of the input file contains an integer T, which denotes the number of test cases. T test cases follow.
The first line of each test case contains an integer N (1 <= N <= 105), which is the number of days. Each of the following N lines contains either "+w" or "?p". Both p and w are 01-string in this problem.
Note that the input file has been encrypted. For each string occured, let L be the result of last "?" operation. The string given to you has been shifted L times (the shifted version of string s1s2 ... sk is sks1s2 ... sk-1). You should decrypt the string to the original one before you process it. Note that L equals to 0 at the beginning of each test case.
The test data guarantees that for each test case, total length of the words does not exceed 105 and total length of the paragraphs does not exceed 5 * 106.
Output
For each test case, first output a line "Case #x:", where x is the case number (starting from 1).
And for each "?" operation, output a line containing the result.
Sample Input
2
3
+01
+01
?01001
3
+01
?010
?011
Sample Output
Case #1:
2
Case #2:
1
0
分析
题目大概说有依次进行N个操作,每个操作可以是学习一个单词,或者读一个段落并求出段落里有多少个子串是已经学习的单词。
建立两个AC自动机,一个大的,一个小的。每次更新插入到小的自动机并重构,小的自动机结点数有限制,一旦超过限制就将其合并到大的,然后大的重构,小的清空。。如此就OK了。。
这么做的时间复杂度——
- 不妨设小的自动机大小限制为$\sqrt L$,$L$为插入的模式串总长,于是最多插入$L$次,每次重构fail时间复杂度可以做到线性的即$O(\sqrt L)$,这样小的自动机这儿总时间复杂度是$O(L\sqrt L)$;
- 对于大的来说,最多的合并次数为$\frac L{\sqrt L}$即$\sqrt L$,每次合并时间复杂度$O(\sqrt L)$,每次重构$O(L)$,那么总的时间复杂度是$O(L\sqrt L)$。
- 而查询,就是在两个AC自动机上跑一遍主串即可,也是可以做到线性的,即$O(L+\sum |主串|)$。
有点神奇。。
代码
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
#define MAXN 100100 struct AC_auto{
int ch[MAXN][2],fail[MAXN],tn;
bool flag[MAXN];
void init(){
for(int i=0; i<=tn; ++i){
ch[i][0]=ch[i][1]=flag[i]=0;
}
tn=0;
}
void insert(char *s){
int x=0;
for(int i=0; s[i]; ++i){
int y=s[i]-'0';
if(ch[x][y]==0) ch[x][y]=++tn;
x=ch[x][y];
}
flag[x]=1;
}
void getfail(){
for(int i=0; i<=tn; ++i) fail[i]=0;
queue<int> que;
for(int i=0; i<2; ++i){
if(ch[0][i]) que.push(ch[0][i]);
}
while(!que.empty()){
int x=que.front(); que.pop();
for(int i=0; i<2; ++i){
if(ch[x][i]==0) continue;
que.push(ch[x][i]);
int tmp=fail[x];
while(tmp && ch[tmp][i]==0){
tmp=fail[tmp];
}
fail[ch[x][i]]=ch[tmp][i];
}
}
}
int match(char *s){
int x=0,ret=0;
for(int i=0; s[i]; ++i){
int y=s[i]-'0';
while(x && ch[x][y]==0) x=fail[x];
x=ch[x][y];
int tmp=x;
while(tmp){
if(flag[tmp]) ++ret;
tmp=fail[tmp];
}
}
return ret;
}
bool query(char *s){
int x=0;
for(int i=0; s[i]; ++i){
int y=s[i]-'0';
if(ch[x][y]==0) return 0;
x=ch[x][y];
}
return flag[x];
}
}ac,buf; void dfs(int u,int v){
for(int i=0; i<2; ++i){
if(buf.ch[v][i]==0) continue;
if(ac.ch[u][i]==0){
ac.ch[u][i]=++ac.tn;
ac.ch[ac.tn][0]=ac.ch[ac.tn][1]=0;
ac.flag[ac.tn]=0;
}
if(buf.flag[buf.ch[v][i]]) ac.flag[ac.ch[u][i]]=1;
dfs(ac.ch[u][i],buf.ch[v][i]);
}
}
void join(){
dfs(0,0);
buf.init();
ac.getfail();
} char str[5111111],s[5111111];
int main(){
int t;
scanf("%d",&t);
for(int cse=1; cse<=t; ++cse){
printf("Case #%d:\n",cse);
ac.init();
buf.init();
int n;
scanf("%d",&n);
int lastans=0;
char op;
while(n--){
scanf(" %c",&op);
scanf("%s",str);
int len=strlen(str);
for(int i=0; i<len; ++i){
s[i]=str[(i+lastans)%len];
}
s[len]=0;
if(op=='+'){
if(ac.query(s) || buf.query(s)) continue;
buf.insert(s);
buf.getfail();
if(buf.tn>2000) join();
}else{
lastans=ac.match(s)+buf.match(s);
printf("%d\n",lastans);
}
}
}
return 0;
}
HDU4787 GRE Words Revenge(AC自动机 分块 合并)的更多相关文章
- GRE Words Revenge AC自动机 二进制分组
GRE Words Revenge 题意和思路都和上一篇差不多. 有一个区别就是需要移动字符串.关于这个字符串,可以用3次reverse来转换, 前面部分翻转一下, 后面部分翻转一下, 最后整个串翻转 ...
- [HDU 4787] GRE Words Revenge (AC自动机)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4787 题目大意: 给你若干个单词,查询一篇文章里出现的单词数.. 就是被我水过去的...暴力重建AC自 ...
- HDU4787 GRE Words Revenge【AC自动机 分块】
HDU4787 GRE Words Revenge 题意: \(N\)次操作,每次记录一个\(01\)串或者查询一个\(01\)串能匹配多少个记录的串,强制在线 题解: 在线的AC自动机,利用分块来降 ...
- HDU 3341 Lost's revenge AC自动机+dp
Lost's revenge Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 65535/65535 K (Java/Others)T ...
- HDU-4787 GRE Words Revenge 解题报告
这是我之前博客里提到的一道AC自动机的练手题,但是要完成这道题,我之前博客里提到的东西还不够,这里总结一下这道题. 这道题不是一般的裸的AC自动机,它的询问和插入是交叉出现的所以用我之前写的板子不大合 ...
- 【CF587F】Duff is Mad AC自动机+分块
[CF587F]Duff is Mad 题意:给出n个串$s_1,s_2..s_n$,有q组询问,每次给出l,r,k,问你编号在[l,r]中的所有串在$s_k$中出现了多少次. $\sum|s_i|, ...
- [HDU4787]GRE Words Revenge 解题报告
这是我之前博客里提到的一道AC自动机的练手题,但是要完成这道题,我之前博客里提到的东西还不够,这里总结一下这道题. 这道题不是一般的裸的AC自动机,它的询问和插入是交叉出现的所以用我之前写的板子不大合 ...
- hdu 4117 GRE Words (ac自动机 线段树 dp)
参考:http://blog.csdn.net/no__stop/article/details/12287843 此题利用了ac自动机fail树的性质,fail指针建立为树,表示父节点是孩子节点的后 ...
- HDU3341 Lost's revenge(AC自动机&&dp)
一看到ACGT就会想起AC自动机上的dp,这种奇怪的联想可能是源于某道叫DNA什么的题的. 题意,给你很多个长度不大于10的小串,小串最多有50个,然后有一个长度<40的串,然后让你将这个这个长 ...
随机推荐
- PL/SQL Developer不配置TNS直接登录
如果只是临时登录,就没必要去配置一个TNS了,Database那里直接输入<IP>:<PORT>/<服务器SERVER_NAME> EBS的直接登录: http:/ ...
- 利用JS实现点击按钮后图片自动切换
我么常常看到一个网站的主界面的图片可以切换自如,那么又是如何实现的呢? 1.HTML页面布局如图所示: Main(div) top(div)(显示需要显示的图片) bottom UL (li)< ...
- Autoit3 获取WinForm下的ToolTip
相比Autohotkey,在我看来,Autoit最实用的就是对于WinForm Application的良好支持 然而,要想将鼠标放在WinForm的ToolTip上,简直无异于自己把自己举起来,故而 ...
- ASP.NET运作流程
当我们在浏览器输入域名访问服务器资源时,会向服务器发送Http请求,并经由IIS处理后,交由ASP.NET托管程序处理,进入ASP.NET管道.在IIS内部如何处理我们不需要深入去了解,在ASP.NE ...
- Java类的加载の动态
类的加载方式 静态加载类,是编译时刻加载 动态加载类,是运行时刻加载 new创建对象:是静态加载类,在编译时刻就需要加载所有的可能使用到的类.有一个类有问题(如不存在),都不能通过编译,会报错. Cl ...
- UML入门
本文主要讲解uml的一些入门知识. uml:统一建模语言,uml通过图形化的表达对系统进行细致的划分,在开发前期有助于开发人员与开发人员之间交流,同时也能方便用户与开发者之间进行良好的反馈.利用uml ...
- JavaFx导出文件
导出文件格式可选 protected void handExportDateAction(ActionEvent event) { // ShowDialog.showConfirmDialog(FX ...
- java并行计算Fork和Join的使用
Java在JDK7之后加入了并行计算的框架Fork/Join,可以解决我们系统中大数据计算的性能问题.Fork/Join采用的是分治法,Fork是将一个大任务拆分成若干个子任务,子任务分别去计算,而J ...
- 关于layoutSubviews
layoutSubviews 是什么? 设定subviews的尺寸和位置,如果要精确布局,可以在子类里重写此方法.不能直接调用此方法,如果想强制layout刷新,调用setNeedsLayout来代替 ...
- linux c system返回值问题总结
先看例子 #include <stdio.h> #include <stdlib.h> #include <sys/wait.h> #include <sys ...