【题目链接】 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自动机)的更多相关文章

  1. 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...

  2. HDU 6138 Fleet of the Eternal Throne(后缀自动机)

    题意 题目链接 Sol 真是狗血,被疯狂卡常的原因竟是 我们考虑暴力枚举每个串的前缀,看他能在\(x, y\)的后缀自动机中走多少步,对两者取个min即可 复杂度\(O(T 10^5 M)\)(好假啊 ...

  3. HDU 6138 Fleet of the Eternal Throne 后缀数组 + 二分

    Fleet of the Eternal Throne Problem Description > The Eternal Fleet was built many centuries ago ...

  4. 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 ...

  5. 2017多校第8场 HDU 6138 Fleet of the Eternal Throne 思维,暴力

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给了初始区间[-1,1],然后有一些操作,可以r加上一个数,l减掉一个数,或者同时操作,问 ...

  6. hdu 6208 The Dominator of Strings【AC自动机】

    hdu 6208 The Dominator of Strings[AC自动机] 求一个串包含其他所有串,找出最长串去匹配即可,但是匹配时要对走过的结点标记,不然T死QAQ,,扎心了.. #inclu ...

  7. hdu 5955 Guessing the Dice Roll 【AC自动机+高斯消元】

    hdu 5955 Guessing the Dice Roll [AC自动机+高斯消元] 题意:给出 n≤10 个长为 L≤10 的串,每次丢一个骰子,先出现的串赢,问获胜概率. 题解:裸的AC自动机 ...

  8. HDU 6208 The Dominator of Strings(AC自动机)

    The Dominator of Strings Time Limit: 3000/3000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java ...

  9. HDU 4057:Rescue the Rabbit(AC自动机+状压DP)***

    http://acm.hdu.edu.cn/showproblem.php?pid=4057 题意:给出n个子串,串只包含‘A’,'C','G','T'四种字符,你现在需要构造出一个长度为l的串,如果 ...

随机推荐

  1. [CodePlus 2017 11月赛]晨跑 题解(辗转相除法求GCD)

    [CodePlus 2017 11月赛]晨跑 Description "无体育,不清华"."每天锻炼一小时,健康工作五十年,幸福生活一辈子".在清华,体育运动绝 ...

  2. PHP 5 MySQLi 函数总结

    连接数据库 mysqli_connect() 函数打开一个到 MySQL 服务器的新的连接. <?php $con=mysqli_connect("localhost",&q ...

  3. Mysql储存过程7: case

    #用在储存过程中: create procedure k() begin declare number int; )); case number then select '>0'; else s ...

  4. python第三方库之numpy基础

    前言 numpy是python的科学计算模块,底层实现用c代码,运算效率很高.numpy的核心是矩阵narray运算. narray介绍 矩阵拥有的属性 ndim属性:维度个数 shape属性:维度大 ...

  5. FileZilla 配置备份与还原【转】

    FileZilla是一款免费开源的FTP软件,安装和配置都很简单.在安装目录下的FileZilla Server Interface.xml和FileZilla Server.xml两个文件是程序的配 ...

  6. 简单实现JS上传图片预览功能

    HTML代码 <div class="upload"> <input type="button" class="btn" ...

  7. c++环境配置 Eclipse+mingw-get-setup

    1,到官网下载eclipse  和  mingw-get-setup 2,先安装eclipse,然后等着... 3,再安装mingw-get-setup, 等待...安装完成后打开,选择basic s ...

  8. Windows: 如何配置IPv6隧道

    清空隧道配置: netsh interface ipv6 set teredo disable netsh interface ipv6 6to4 set state disable netsh in ...

  9. Oracle 序列(sequence)的创建、修改及删除

    1.Oracle 创建序列化:create sequence xxxx create sequence student_id minvalue --最小值 nomaxvalue --不设置最大值(由机 ...

  10. python爬取网易云音乐歌单音乐

    在网易云音乐中第一页歌单的url:http://music.163.com/#/discover/playlist/ 依次第二页:http://music.163.com/#/discover/pla ...