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的串,如果 ...
随机推荐
- Big O
评价一个计算机算法的效率时用到的方法我们称之为Big O(Order of [大约是]). 有序二分查找算法: 比如我们通过二分查找算法查找容器里的10个数据. 那么,我们需要查找一个数据时正常应该是 ...
- UML相关说明
在java开发中,有很多时候我们是在不断的处理类与类之间关系,其中这六种关系是:依赖.关联.聚合.组合.继承.实现. 它们的强弱关系是没有异议的:依赖 < 关联 < 聚合 < 组合& ...
- Django【设计】同功能不同实现模式的兼容性
需求: 当我们采集硬件信息时,客户端可以有多种方式,具体方式取决于客户机,CMDB项目中,我们有三种方式可选,AGENT/SSH/SALT,根据客户机具体情况和“SALT>>SSH> ...
- 64_p5
php-nette-bootstrap-2.4.3-1.fc26.noarch.rpm 20-Feb-2017 07:19 16290 php-nette-caching-2.5.3-1.fc26.n ...
- Machine Learning系列--归一化方法总结
一.数据的标准化(normalization)和归一化 数据的标准化(normalization)是将数据按比例缩放,使之落入一个小的特定区间.在某些比较和评价的指标处理中经常会用到,去除数据的单位限 ...
- Apache虚拟主机配置(多个域名访问多个目录)(转)
Apache虚拟主机配置(多个域名访问多个目录) 为了方便管理虚拟主机,我决定使用一种方法,那就是修改httpd-vhosts.conf文件. 第一步首先要使扩展文件httpd-vhosts.conf ...
- u-boot中的Makefile
在windos下,pc机上电之后,BIOS会初始化硬件配置,为内核传递参数,引导操作系统启动,并且识别C盘.D盘.等整个操作系统启动起来之后,才可以运行应用程序比如QQ.QQ音影.同理,在嵌入式Lin ...
- Java的Timer定时器
Timer主要用于Java线程里指定时间或周期运行任务,它是线程安全的,但不提供实时性(real-time)保证. 上面提到了守护线程的概念. Java分为两种线程:用户线程和守护线程. 所谓守护线程 ...
- mysql视图学习总结(转)
一.使用视图的理由是什么?1.安全性.一般是这样做的:创建一个视图,定义好该视图所操作的数据.之后将用户权限与视图绑定.这样的方式是使用到 了一个特性:grant语句可以针对视图进行授予权限.2.查询 ...
- jekyll简单使用
jekyll build # => 当前文件夹中的内容将会生成到 ./site 文件夹中. jekyll build –destination <destination> # =&g ...