HDU4787_GRE Words Revenge
这个题目做得泪牛满面。
题目为给你若干串,有的表示添加一个串,有的表示询问一个串有多少个字串为前面出现过的串。
题目一看就知道肯定是AC自动机(不过后缀自动机也是可以的)
但是细想就会发现AC自动机好像不支持在线修改。如果你每次读入一个串就重构一次AC自动机的话,那么时间复杂度达到了N^2,肯定会T的。
于是就产生了一种奇葩的解法。
搞两个自动机,一个自动机为大的自动机,一个自动机为小的自动机(用于缓冲)。每次我都只把字符串加入到小的自动机里面并且重构小自动机,当小自动机的容量超过了sqrt(L)的时候,我们把小自动机合并到大自动机上,这样算下来时间复杂度为O(L*sqrt(L)),每次询问的答案就是大小自动机上查询答案的和,好像是可以AC的。
但是问题来了,怎么合并两个自动机呢?
其实很简单,自动机相对于字典树来说有什么区别呢?一个是tire树,一个是tire图,自动机多了fail指针还多了一些新建立的next指针。所以我们只要把这些多余的指针去掉,然后对小字典树做一次搜索就可以吧两个字典树合并了。然后重构了一次大自动机,小自动机清空,就可以了。
总的时间复杂度有点勉强,不过算下来10^5好像理论上来说也是可以过的。
赞一个,题目出的太好了。
不过听说还有用后缀自动机做,并且用动态树来维护的。不明觉厉啊。求神犇指点。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#define maxn 100050
using namespace std; int next[][maxn][],has[][maxn][],tag[][maxn],sum[][maxn],fail[][maxn],N[];
char s0[*maxn],s[*maxn];
int qx[maxn],qy[maxn],tq;
int ans,n,t; int add(int x)
{
N[x]++;
next[x][N[x]][]=next[x][N[x]][]=;
has[x][N[x]][]=has[x][N[x]][]=;
tag[x][N[x]]=;
sum[x][N[x]]=;
fail[x][N[x]]=;
return N[x];
} void init(int x)
{
N[x]=-;
N[x]=add(x);
} void initall()
{
ans=;
init(),init();
} void getstring()
{
scanf("%s",s0);
int L=strlen(s0)-,cur=;
for (int i=ans%L+; s0[i]; i++) s[cur]=s0[i],cur++;
for (int i=; i<=ans%L; i++) s[cur]=s0[i],cur++;
s[L]=;
} void destory(int x)
{
for (int i=; i<=N[x]; i++)
{
fail[x][i]=;
for (int j=; j<; j++)
if (has[x][i][j]==) next[x][i][j]=;
}
} void insert()
{
int cur=,tep;
for (int i=; s[i]; i++)
{
tep=s[i]-'';
if (next[][cur][tep]==)
{
next[][cur][tep]=add();
has[][cur][tep]=;
}
cur=next[][cur][tep];
}
tag[][cur]=;
} void union01()
{
int cur=;
qx[]=qy[]=;
while (cur>)
{
int xx=qx[cur],yy=qy[cur];
if (tag[][yy]) tag[][xx]=;
cur--;
for (int i=; i<; i++)
{
if (next[][yy][i])
{
if (next[][xx][i]==)
{
next[][xx][i]=add();
has[][xx][i]=;
}
cur++;
qx[cur]=next[][xx][i];
qy[cur]=next[][yy][i];
}
}
}
} void AC_build(int x)
{
for (int i=; i<=N[x]; i++) sum[x][i]=tag[x][i];
queue<int> Q;
int cur,child;
Q.push();
while (!Q.empty())
{
cur=Q.front(),Q.pop();
for (int i=; i<; i++)
{
child=next[x][cur][i];
if (child)
{
Q.push(child);
if (cur==) fail[x][child]=;
else
{
fail[x][child]=next[x][fail[x][cur]][i];
sum[x][child]+=sum[x][fail[x][child]];
}
}
else next[x][cur][i]=next[x][fail[x][cur]][i];
}
}
} int query(int x)
{
int cur=,tep,tot=;
for (int i=; s[i]; i++)
{
tep=s[i]-'';
cur=next[x][cur][tep];
tot+=sum[x][cur];
}
return tot;
} bool find(int x)
{
int cur=,tep;
for (int i=; s[i]; i++)
{
tep=s[i]-'';
if (has[x][cur][tep]) cur=next[x][cur][tep];
else return false;
}
if (tag[x][cur]==) return false;
return true;
} int main()
{
int cas=;
scanf("%d",&t);
while (t--)
{
printf("Case #%d:\n",++cas);
initall();
scanf("%d",&n);
while (n--)
{
getstring();
if (s0[]=='+')
{
if (find() || find()) continue;
destory();
insert();//insert s to trie 1
if (N[]>=)
{
destory();
union01();
AC_build();
init();
}
else AC_build();
}
else if (s0[]=='?')
{
ans=query()+query();
printf("%d\n",ans);
}
}
}
return ;
}
HDU4787_GRE Words Revenge的更多相关文章
- 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 5019 Revenge of GCD(数学)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 Problem Description In mathematics, the greatest ...
- L - Abbott's Revenge(比较复杂的bfs)
Time Limit:3000MS Memory Limit:0KB 64bit IO Format:%lld & %llu Submit Status Practice UV ...
- hdu 4099 Revenge of Fibonacci 大数+压位+trie
最近手感有点差,所以做点水题来锻炼一下信心. 下周的南京区域赛估计就是我的退役赛了,bless all. Revenge of Fibonacci Time Limit: 10000/5000 MS ...
- HDU5088——Revenge of Nim II(高斯消元&矩阵的秩)(BestCoder Round #16)
Revenge of Nim II Problem DescriptionNim is a mathematical game of strategy in which two players tak ...
- HDU5087——Revenge of LIS II(BestCoder Round #16)
Revenge of LIS II Problem DescriptionIn computer science, the longest increasing subsequence problem ...
- HDU5086——Revenge of Segment Tree(BestCoder Round #16)
Revenge of Segment Tree Problem DescriptionIn computer science, a segment tree is a tree data struct ...
- UVA 816 - Abbott's Revenge(BFS)
UVA 816 - Abbott's Revenge option=com_onlinejudge&Itemid=8&page=show_problem&category=59 ...
- hdu 4099 Revenge of Fibonacci Trie树与模拟数位加法
Revenge of Fibonacci 题意:给定fibonacci数列的前100000项的前n位(n<=40);问你这是fibonacci数列第几项的前缀?如若不在前100000项范围内,输 ...
随机推荐
- PhpStorm2016.2版本 安装与破解
1.PhpStorm2016简介以及下载地址 1.1.PhpStorm介绍 PhpStorm是一个轻量级且便捷的PHP IDE,其旨在提高用户效率,可深刻理解用户的编码,提供智能代码补全 快速导 ...
- #20155319 2016-2017-2 《Java程序设计》第3周学习总结
20155319 2016-2017-2 <Java程序设计>第3周学习总结 教材学习内容总结 第三周的学习量还是很大的,需要学习的内容更难了而且 量也变多了,所以投入了更多的时间到Jav ...
- 20155325 实验三 敏捷开发与XP实践
实验三 敏捷开发与XP实践-1 http://www.cnblogs.com/rocedu/p/4795776.html, Eclipse的内容替换成IDEA 在IDEA中使用工具(Code-> ...
- 20155325 2016-2017-2 《Java程序设计》第1周学习总结
教材学习内容总结 三大平台 java SE:JVM(java虚拟机),JRE(运行java程序),JDK(包括JRE及开发过程中需要的一些工具程序),java语言. java EE:基于java SE ...
- 学号 2016-2017-20155329《Java程序设计》课程总结
学号 2016-2017-20155329<Java程序设计>课程总结 (按顺序)每周作业链接汇总 预备作业1:想象中的师生关系 预备作业2:C语言水平调查以及认为自己最强的一项技能和毕业 ...
- 实验一:实现求正整数1-N之间所有质数的功能,并进行测试。
实验一 Java开发环境的熟悉(Linux + Eclipse) 实验内容 1.使用JDK编译.运行简单的Java程序: 2.使用Eclipse 编辑.编译.运行.调试Java程序. 命令行下的程序开 ...
- 20155332 2006-2007-2 《Java程序设计》第3周学习总结
学号 2006-2007-2 <Java程序设计>第3周学习总结 教材学习内容总结 尽量简单的总结一下本周学习内容 尽量不要抄书,浪费时间 看懂就过,看不懂,学习有心得的记一下 教材学习中 ...
- pycharm设置github
1.打开file,选择settings,找到Version Contorl,打开找到GitHub ,HOST填github.com,用户名,密码,test,稍等一会,会提示成功 2. 设置好以后 打开 ...
- 「Leetcode」13. Roman to Integer(Java)
分析 把具体的情况一个一个实现即可,没有什么幺蛾子. 代码 class Solution { public int romanToInt(String s) { int ans = 0; for (i ...
- selenium自动化之加载浏览器的配置文件
做seleniumUI自动化关于选用哪个浏览器方面,对于我来说,火狐浏览器只是用于定位元素,因为有firebug(注意高版本的火狐已经安装不了这个插件了),而真正执行自动化脚本用的是谷歌,感觉谷歌的速 ...