【题目链接】 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. 【转】CentOS7 yum方式配置LAMP环境

    采用Yum方式搭建: Apache+Mysql+PHP环境 原文地址: http://www.cnblogs.com/zutbaz/p/4420791.html 1.安装Apache yum inst ...

  2. 笔记本自开wifi设置

    笔记本自开wifi设置 是这样的有些笔记本他自身就可以放出热点供其他的小伙伴们连接,不用非得去下专门的工具有些笔记本的网卡是自带支持双收发的(这里注意我指的是有些笔记本不是全部) 命令我已经写出来了  ...

  3. SQLite3 安装、基本操作

    1. 安装SQLite3 sudo apt-get install sqlite3 2. 安装Sqlite3编译需要的工具包 如果,你需要的话可以安装该工具包.只是为了体验一把,可以不安装.该项是可选 ...

  4. python使用unittest模块selenium访问斗鱼获取直播信息

    import unittest from selenium import webdriver from bs4 import BeautifulSoup as bs class douyu(unitt ...

  5. 用于启动 Windows Phone 8 内置应用的 URI 方案

    本主题列出了可用于启动内置应用的 URI 方案.许多内置于 Windows Phone 的应用,都可以通过调用 LaunchUriAsync(Uri) 和传入一个使用与要启动应用相关的方案的 URI, ...

  6. leetcode 之Median of Two Sorted Arrays(五)

    找两个排好序的数组的中间值,实际上可以扩展为寻找第k大的数组值. 参考下面的思路,非常的清晰: 代码: double findMedianofTwoSortArrays(int A[], int B[ ...

  7. 20165301 2017-2018-2 《Java程序设计》第八周学习总结

    20165301 2017-2018-2 <Java程序设计>第八周学习总结 教材学习内容总结 第十二章:Java多线程机制 进程与线程 操作系统与进程:进程是程序的一次动态执行过程. 进 ...

  8. JAVA实现图的邻接表以及DFS

    一:定义邻接表结构储存图 package 图的遍历; //邻接表实现图的建立 //储存边 class EdgeNode { int index; // 习惯了用index,其实标准写法是(adjVer ...

  9. 简易web server之python实现

    网络编程一项基本功是socket编程,包括TCP socket,UDP socket的客户端.服务器端编程. 应用层的各路协议如http,smtp,telnet,ftp等都依赖于传输层的TCP或者UD ...

  10. 【hdoj_1753】大明A+B(大数)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1753 本题要求是,进行多位的小数加法,由于位数很多,所以不能用double类型存储,可以用字符串存储,然后 ...