HDU 6138 Fleet of the Eternal Throne(AC自动机)
【题目链接】 http://acm.hdu.edu.cn/showproblem.php?pid=6138
【题目大意】
给出一些串,询问第x个串和第y个串的公共子串,
同时要求该公共子串为某个串的前缀。求最长符合要求的答案
【题解】
我们对所有串构建AC自动机,将两个询问串之一在AC自动机上mark所有的匹配位置
另一个串在mark的地方寻找最长匹配即可
【代码】
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=100010;
int ans;
namespace AC_DFA{
const int Csize=27;
int tot,son[N][Csize],sum[N],fail[N],q[N],dph[N],vis[N];
void Initialize(){
memset(dph,0,sizeof(int)*(tot+1));
memset(fail,0,sizeof(int)*(tot+1));
memset(sum,0,sizeof(int)*(tot+1));
for(int i=0;i<=tot;i++)for(int j=0;j<Csize;j++)son[i][j]=0;
tot=0; fail[0]=-1;
}
inline int Tr(char ch){return ch-'a';}
int Insert(char *s){
int x=0;
for(int l=strlen(s),i=0,w;i<l;i++){
if(!son[x][w=Tr(s[i])]){
son[x][w]=++tot;
dph[tot]=i+1;
}x=son[x][w];
}sum[x]++;
return x;
}
void MakeFail(){
int h=1,t=0,i,j,x;
for(i=0;i<Csize;i++)if(son[0][i])q[++t]=son[0][i];
while(h<=t)for(x=q[h++],i=0;i<Csize;i++)
if(son[x][i]){
fail[son[x][i]]=son[fail[x]][i],q[++t]=son[x][i];
}else son[x][i]=son[fail[x]][i];
}
void Cal(char *s){
memset(vis,0,sizeof(vis));
for(int l=strlen(s),i=0,x=0,w;i<l;i++){
while(!son[x][Tr(s[i])])x=fail[x];
x=son[x][Tr(s[i])];
for(int j=x;j;j=fail[j])vis[j]=1;
}
}
void Find(char *s){
for(int l=strlen(s),i=0,x=0,w;i<l;i++){
while(!son[x][Tr(s[i])])x=fail[x];
x=son[x][Tr(s[i])];
for(int j=x;j;j=fail[j])if(vis[j])ans=max(ans,dph[j]);
}
}
}
char s[110][N];
int T,n;
int main(){
scanf("%d",&T);
while(T--){
scanf("%d",&n);
using namespace AC_DFA;
Initialize();
for(int i=1;i<=n;i++){
scanf("%s",s[i]);
Insert(s[i]);
}int q;
MakeFail();
scanf("%d",&q);
while(q--){
int x,y; ans=0;
scanf("%d%d",&x,&y);
Cal(s[x]); Find(s[y]);
printf("%d\n",ans);
}
}return 0;
}
HDU 6138 Fleet of the Eternal Throne(AC自动机)的更多相关文章
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...
- HDU 6138 Fleet of the Eternal Throne(后缀自动机)
题意 题目链接 Sol 真是狗血,被疯狂卡常的原因竟是 我们考虑暴力枚举每个串的前缀,看他能在\(x, y\)的后缀自动机中走多少步,对两者取个min即可 复杂度\(O(T 10^5 M)\)(好假啊 ...
- HDU 6138 Fleet of the Eternal Throne 后缀数组 + 二分
Fleet of the Eternal Throne Problem Description > The Eternal Fleet was built many centuries ago ...
- 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)
题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne 思维,暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给了初始区间[-1,1],然后有一些操作,可以r加上一个数,l减掉一个数,或者同时操作,问 ...
- hdu 6208 The Dominator of Strings【AC自动机】
hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...
- hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】
hdu 5955 Guessing the Dice Roll [AC自动机+高斯消元] 题意:给出 n≤10 个长为 L≤10 的串,每次丢一个骰子,先出现的串赢,问获胜概率. 题解:裸的AC自动机 ...
- HDU 6208 The Dominator of Strings(AC自动机)
The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others) Memory Limit: 65535/32768 K (Java ...
- HDU 4057:Rescue the Rabbit(AC自动机+状压DP)***
http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...
随机推荐
- jQuery实现简单前端搜索功能
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 35 - 并发编程-GIL-多进程
目录 1 GIL 1.1 为什么会有GIL 1.2 GIL与thread lock 1.3 个人总结 2 multiprocessing模块 2.1 Process类 2.2 Process类的方法 ...
- http 之cookie和session
cookie和session 关于http: 1.http是:无状态.短连接 2.http的请求生命周期:给服务端发送一个请起头,通过域名提取url,通过路由关系匹配,再通过函数+html进行模板加 ...
- 大数据系列之kafka-java实现
Java源码GitBub地址: https://github.com/fzmeng/kafka-demo 关于kafka安装步骤可见文章 http://www.cnblogs.com/cnmeng ...
- 生成器(generator)和迭代(iterable , iterator, iteration)
在搞清楚Generator之前,我们先讨论一下 iterable , iterator, iteration 1.Iterable 我们知道,在Python中所有东西都是object, 比如说变量,容 ...
- php 全文搜索解决方法
全套解决方案 xunsearch 一.安装编译工具 yum install make gcc g++ gcc-c++ libtool autoconf automake imake mysql-dev ...
- Netty并发优化之ExecutionHandler
上文<Netty框架入门>说到:如果业务处理handler耗时长,将严重影响可支持的并发数. 针对这一问题,经过学习,发现了可以使用ExecutionHandler来优化. 先来回顾一下没 ...
- vsftpd.conf 详解
//不允许匿名访问 anonymous_enable=NO //设定本地用户可以访问.注意:主要是为虚拟宿主用户,如果该项目设定为NO那么所有虚拟用户将无法访问 local_enable=YES // ...
- Builder设计模式--改善构造器多个参数时可显著改善可读性
作为一名程序开发者,设计模式其实一直有在接触,只是没有专门的去学过,所以可能对设计模式没有一个系统的理解.在一次项目中,需要使用到第三方服务商提供的功能,为了尽快的熟悉其功能代码,在官网下了demo来 ...
- mini_httpd在RedHat 5下安装
1.安装mini_httpdcd /usr/src/redhat/SOURCES wget http://www.acme.com/software/mini_httpd/mini_httpd-1.1 ...