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项范围内,输 ...
随机推荐
- 20155307 2016-2017-2 《Java程序设计》第4周学习总结
20155307 2016-2017-2 <Java程序设计>第4周学习总结 教材学习内容总结 所谓继承,讲的就是出现很多很多相同的部分的话,就把这个部分变成"父类", ...
- 20155319 2016-2017-2 《Java程序设计》第一周学习总结
20155319 2016-2007-2 <Java程序设计>第一周学习总结 考核方式 翻转课堂考核12次(5*12=60):每次考试20-30道题目,考试成绩规格化成5分(如总分20分就 ...
- hasOwnProperty()函数
hasOwnProperty()函数的返回值为Boolean类型.如果对象object具有名称为propertyName的属性,则返回true,否则返回false 例子: function Site( ...
- 成都Uber优步司机奖励政策(4月12日)
滴快车单单2.5倍,注册地址:http://www.udache.com/ 如何注册Uber司机(全国版最新最详细注册流程)/月入2万/不用抢单:http://www.cnblogs.com/mfry ...
- pyqt5 菜单,工具栏,线程,matplotlib
import sys from PyQt5.QtGui import QIcon from PyQt5.QtWidgets import QMainWindow, QMenuBar, QToolBar ...
- Luogu3804 【模板】后缀自动机
题面 题解 一个串的出现次数等于$endpos$的大小,也是$parent$树上节点的$size$大小, 构建出后缀自动机,按拓补序,模拟即可. 代码 #include<cstdio> # ...
- 【索引】MySQL索引
一.索引的定义及作用 1. 二.索引的创建及删除 1.1查看表的索引 show index from tblname; 1.2.创建索引 1.22创建普通索引 ALTER TABLE `table_n ...
- [备忘]Windows Server 2008 R2部署FTP FileZilla Server防火墙设置
有一台服务器,之前文件迁移少,现准备用FileZilla Server当FTP服务器,服务器系统是Windows Server 2008 R2,同样适用FileZilla Client连接服务器FTP ...
- datax 执行流程分析
https://www.jianshu.com/nb/29319571 https://www.jianshu.com/p/b10fbdee7e56
- Sublime Text 2 - Unable to find git.exe 错误
今日打开 Sublime Text 2,随即弹出 Package Control - Unable to find git.exe 错误.如下, 原因:曾经通过 git clone 命令获取过 Sub ...