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的串,如果 ...
随机推荐
- wampserver 虚拟主机
转载:http://blog.csdn.net/knight_quan/article/details/51830683 1.背景: 在进行网站开发的时候,通常需要以http://localhost或 ...
- python基础===将Flask用于实现Mock-server
from flask import Flask from flask import request, Response, jsonify import random import string app ...
- MySQL之——如何添加新数据库到MySQL主从复制列表 【转】
转自 转载请注明出处:http://blog.csdn.net/l1028386804/article/details/54653691 MySQL主从复制一般情况下我们会设置需要同步的数据库,使用参 ...
- HTTPS握手过程
HTTPS在HTTP的基础上加入了SSL协议,SSL依靠证书来验证服务器的身份,并为浏览器和服务器之间的通信加密.具体是如何进行加密,解密,验证的,且看下图,下面的称为一次握手. 1. 客户端发起HT ...
- HTML文件编码
为了防止中文乱码,一般在网页头文件中加入 <meta http-equiv="Content-Type" content="text/html; charset=u ...
- UFT12.续期的操作方法
安装完毕UFT后,页面中报install错误,此时报此错误的原因是因为UFT的许可证过期了,解决方法如下: 方法是找到C:\ProgramData目录下的SafeNet Sentinel文件夹将其删除 ...
- udev和rules使用规则
本文以通俗的方法阐述 udev 及相关术语的概念.udev 的配置文件和规则文件,然后以 Red Hat Enterprise Server 为平台演示一些管理设备文件和查询设备信息的实例.本文会使那 ...
- linux中的vim 编辑一行内容,如何使光标快速移动到行首和行尾以及行中间某处啊?
光标定位G 移至行行首nG 移至第n行行首n+ 移n行行首n- 移n行行首n$ 移n行(1表示本行)行尾0 所行行首$ 所行行尾^ 所行首字母h,j,k,l 左移移移右移H 前屏幕首行行首M 屏幕显示 ...
- php极速后台开发框架LotusAdmin
组件:基于thinkphp5.0.12+layui2.1版本 演示站点:https://www.lotusadmin.top/账号 : admin密码:123456 官方QQ交流群:606645328 ...
- ubuntu包管理命令apt和dpkg的用法
apt-get命令: apt-get是debian,ubuntu发行版的包管理工具,与红帽中的yum工具非常类似,适用于deb包管理式的操作系统,主要用于自动从互联网的软件仓库中搜索.安装.升级.卸载 ...