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第三次《Java程序设计》课堂实践项目
码云链接 我使用电脑端登录的云班课,截图已经放上去了,却忘了点提交.
- 2017 ACM-ICPC 亚洲区(西安赛区)网络赛
A There is a tree with nn nodes, at which attach a binary 64*6464∗64 matrix M_i (1 \le i \le n)M i ...
- 20145226夏艺华 《Java程序设计》第7&8周学习总结、实验一
[实验一]http://www.cnblogs.com/bestixyh/p/6358734.html [第7周]http://www.cnblogs.com/bestixyh/p/6380475.h ...
- Intellij IDEA 2017 通过scala工程运行wordcount
首先是安装scala插件,可以通过idea内置的自动安装方式进行,也可以手动下载可用的插件包之后再通过idea导入. scala插件安装完成之后,新建scala项目,右侧使用默认的sbt 点击Next ...
- arcpy示范教学(一):基本操作
arcpy基本操作 打开目录,遍历目录,打开要素类,遍历要素,打开文件,写入属性值 import arcpy import codecs # 设置工作目录 arcpy.env.workspace = ...
- 我们一起学习WCF 第五篇数据协定和消息协定
A:数据协定(“数据协定”是在服务与客户端之间达成的正式协议,用于以抽象方式描述要交换的数据. 也就是说,为了进行通信,客户端和服务不必共享相同的类型,而只需共享相同的数据协定. 数据协定为每个参数或 ...
- LUIS 语义识别API调用方法
本例使用itchat获取微信文字消息,发送给LUIS返回识别消息,再将返回消息格式化后通过微信发回 关于itchat的使用参考我的另外一篇随笔itchat个人练习 语音与文本图灵测试例程 # -*- ...
- TW实习日记:第十天
今天任务很简单,就是出品项目的时间轴显示页面和动态路由设置.其实时间轴页面很快就做完了,在做完处理完数据之后,然而有很多细节需要打磨,这就又考验了我面向搜索引擎编程的能力,根据需求百度了很多css的样 ...
- centos 6.5 双网卡 上网 virtualbox nat hostonly
虚拟机两张网卡:分别调成NAT(eth0)和host only(eht1)模式. nat的网卡不用设置,host only网卡调为(vi /etc/sysconfig/network-scripts/ ...
- ASP.NET MVC - 启动创建项目,未能加载错误
VS2012以常规方式创建一ASP.NET MVC internet 项目.创建后F5启动项目,遇一错误: 未能加载文件或程序集“MySql.Web.v20, Version=6.9.4.0, Cul ...