hdu_4787_GRE Words Revenge(在线AC自动机)
题目链接:hdu_4787_GRE Words Revenge
题意:
总共有n个操作,2种操作。每行读入一个字符串。
1.如果字符串以+开头,此为单词(即模式串,不考虑重复)
2.如果字符串以?开头,此为文章(即文本串,查询在此之前的单词在文本串中出现的次数)
题解:
强制在线的AC自动机
贴个大牛的详细题解http://blog.csdn.net/no__stop/article/details/16823479
这样的带合并操作的AC自动机用第二种建树的方式比较方便
#include<bits/stdc++.h>
#define F(i,a,b) for(int i=a;i<=b;i++)
using namespace std; const int AC_N=1e5+,tyn=,M=5e6+;//数量乘串长,类型数
struct AC_automation{
int tr[AC_N][tyn],cnt[AC_N],Q[AC_N],fail[AC_N],tot;
void nw(){cnt[++tot]=;memset(tr[tot],-,sizeof(tr[tot]));}
void init(){tot=-,fail[]=-,nw();}
void insert(char *s,int x=){
for(int i=,w;s[i];x=tr[x][w],i++)
if(tr[x][w=s[i]-'']==-)nw(),tr[x][w]=tot;
cnt[x]=;//串尾标记
}
void build(int head=,int tail=){
F(i,,)if(~tr[][i])Q[++tail]=tr[][i],fail[tr[][i]]=;
while(head<=tail){
for(int i=,x=Q[head++],p=-;i<tyn;i++)if(~tr[x][i]){
for(p=fail[x],fail[tr[x][i]]=;~p;p=fail[p])
if(~tr[p][i]){fail[tr[x][i]]=tr[p][i];break;}
Q[++tail]=tr[x][i];
}
}
}
int ask(char *s,int ans=){
for(int w,i=,x=,j;s[i];i++){
while(tr[x][w=s[i]-'']==-&&x)x=fail[x];
x=tr[x][w],x=(~x)?x:,j=x;
while(j){if(cnt[j])ans++;j=fail[j];}
}
return ans;
} }a,b; char s[M],tps[M];
int t,n,an,sqr=,bcnt; void dfs(int r1,int r2)//将b中以r2为根结点的树合并到a中以r1为根结点的树中
{
F(i,,)if(~b.tr[r2][i])
{
if(a.tr[r1][i]==-)a.nw(),a.tr[r1][i]=a.tot;
a.cnt[a.tr[r1][i]]|=b.cnt[b.tr[r2][i]];
dfs(a.tr[r1][i],b.tr[r2][i]);
}
} void join(){dfs(,),b.init(),a.build();}//将b合并到a中 void decrypt()
{
int len=strlen(s);
int k=an%(len-),ed=;
F(i,,len-)tps[i]=s[i];
F(i,k+,len-)s[++ed]=tps[i];
F(i,,k)s[++ed]=tps[i];
} int main()
{
scanf("%d",&t);
F(ic,,t)
{
printf("Case #%d:\n",ic);
scanf("%d",&n);
a.init(),b.init(),an=bcnt=;
F(i,,n)
{
scanf("%s",s),decrypt();
if(s[]=='+')
{
b.insert(s+),b.build(),bcnt++;
if(bcnt>sqr)join();
}else printf("%d\n",(an=a.ask(s+)+b.ask(s+)));
}
}
return ;
}
hdu_4787_GRE Words Revenge(在线AC自动机)的更多相关文章
- HDU4787 GRE Words Revenge【AC自动机 分块】
HDU4787 GRE Words Revenge 题意: \(N\)次操作,每次记录一个\(01\)串或者查询一个\(01\)串能匹配多少个记录的串,强制在线 题解: 在线的AC自动机,利用分块来降 ...
- CodeForces - 710F:String Set Queries (二进制分组 处理 在线AC自动机)
ou should process m queries over a set D of strings. Each query is one of three kinds: Add a string ...
- HDU4787 GRE Words Revenge(AC自动机 分块 合并)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=4787 Description Now Coach Pang is preparing for ...
- CodeForces 710F 强制在线AC自动机
题目链接:http://codeforces.com/contest/710/problem/F 题意:维护一个集合,集合要求满足三种操作. 1 str:向集合插入字符串str(保证不会插入之前已经插 ...
- 【HDOJ3341】Lost's revenge(AC自动机,DP)
题意:给出一个n个模式串,一个目标串,问把目标串重新排位最多能产生多少个模式串,可以重叠且所有串只包含A C G T. n<=10,len[i]<=10 len(s)<=40 Cas ...
- 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 ...
- 【HDU3341】 Lost's revenge (AC自动机+状压DP)
Lost's revenge Time Limit: 5000MS Memory Limit: 65535KB 64bit IO Format: %I64d & %I64u Descripti ...
- HDU3341 Lost's revenge(AC自动机+DP)
题目是给一个DNA重新排列使其包含最多的数论基因. 考虑到内存大概就只能这么表示状态: dp[i][A][C][G][T],表示包含各碱基个数为ACGT且当前后缀状态为自动机第i的结点的字符串最多的数 ...
- HDU 3341 Lost's revenge (AC自动机 + DP + 变进制/hash)题解
题意:给你些分数串,给你一个主串,主串每出现一个分数串加一分,要你重新排列主串,最多几分 思路:显然这里开$40^4$去状压内存不够.但是我们自己想想会发现根本不用开那么大,因为很多状态是废状压,不是 ...
随机推荐
- 分离JavaScript
分离JavaScript类似于使用style属性,在HTML文档里使用诸如onclick之类的属性也是一种既没有效率又容易引发问题的做法.如果我们用一个"挂钩",就像CSS机制中的 ...
- java系统参数
package com.test; import java.sql.SQLException; import java.util.Properties; import com.mchange.v2.c ...
- 关于Python的文件IO
使用Open函数, 第一个参数为文件名, 例如"C:\abc.txt",这里要注意的是r"C:\abc.txt". 第二个参数为文件的操作方式, 这里着重探讨写 ...
- WPF和Winform的一些界面控件
DevExpressTelerikMahApps.MetroModern UI for WPFModernWPFExtended WPF Toolkit™ Community EditionModer ...
- Linux网络管理常用命令:net-tools VS iproute2
Exported from workflowy! net-tools ifconfigifconfig命令:用于接口及地址查看和管理 route netstat arp iproute2 ip lin ...
- 关于ajax的短轮询问题
利用前台的ajax不断向后台服务器请求,后台服务器不断查看数据库里的信息是否变化.若变化将信息返回前台,并执行一些操作 前台ajax代码 注意要加上cache这一项,如果是post请求的化,可以免了. ...
- ONE WIRE
以温度温度传感器为例 由三根线,分别为电源,信号,地线 使用GPIO口对信号线进行读操作 //初始化GPIO PC0端口void dht11_init(){ GPIO_InitTypeDef GPIO ...
- JavaScript-Curry
Currying allows you to easily create custom functions by partially invoking an existing function. He ...
- xampp版本和具体的php,mysql版本的对应
在国外网上查找到具体的xampp版本与php,mysql版本的对应关系,特此记录.以便需要的人使用.原文链接如下: http://code.stephenmorley.org/articles/xam ...
- ie6的png24问题
解决IE6的PNG透明JS插件 DD_belatedPNG 引:http://www.cnblogs.com/cobby/archive/2012/05/11/2495801.html IE6的PNG ...