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项范围内,输 ...
随机推荐
- 20155230 实验二《Java面向对象程序设计》实验报告
20155230 实验二<Java面向对象程序设计>实验报告 一.单元测试 三种代码 知道了伪代码.产品代码.测试代码的关系和用途,并根据老师的例子,按测试代码调试了产品代码. 值得注意的 ...
- 20155235 2017-2018-1 《Java程序设计》第2周学习总结
20155235 2017-2018-1 <Java程序设计>第2周学习总结 教材学习内容总结 3.1类型.变量与运算符 类型 基本类型 类类型 变量 基本规则 不可以用数字作为开头,不可 ...
- tomcat如何禁止显示目录和文件列表
打开 tomcat的安装目录/conf/web.xml 文件 找到: <servlet> <servlet-name>default</servlet-name& ...
- day2 HTML - body
<body>内常用标签 1.基本标签 所有标签分为: # 块级标签: div(白板),H系列(加大加粗),p标签(段落和段落之间有间距) # 行内标签: span(白板) 1. 图标, ...
- angular中的$http服务
$http是ng内置的一个服务.是简单的封装了浏览器原生的XMLHttpRequest对象. 写法1 $http({ method: "GET", url: 'data.json' ...
- 同一个电脑配置两个Git问题
拿到公司电脑后,正常配置gitlab,以及设置邮箱等等,可以使用公司邮箱,以及一系列设置 git config --global user.name "userName" git ...
- appium自动化环境搭建
1.java开发环境JDK 2.android SDK(platform/platform tools/tools/build tools) 3.python下载安装(pip) 4.appium下载安 ...
- sublime3配置java开发环境
链接:http://www.jianshu.com/p/48a524a4f63c 或者:http://www.jianshu.com/p/9d167c4c4feb 侵权删!
- Selenium WebDriver 下 plugin container for firefox has stopped working
用selenium 的webdriver 和 firefox 浏览器做自动化测试,经常会出现 plugin container for firefox has stopped working 如下图所 ...
- JS加密库
作者声明:本博客中所写的文章,都是博主自学过程的笔记,参考了很多的学习资料,学习资料和笔记会注明出处,所有的内容都以交流学习为主.有不正确的地方,欢迎批评指正 本文主要是参考aicoder马伦老师的博 ...