HDU 6138 Fleet of the Eternal Throne(后缀自动机)
题意
Sol
真是狗血,被疯狂卡常的原因竟是

我们考虑暴力枚举每个串的前缀,看他能在\(x, y\)的后缀自动机中走多少步,对两者取个min即可
复杂度\(O(T 10^5 M)\)(好假啊)
#include<bits/stdc++.h>
using namespace std;
const int MAXN = 2e5 + 10;
int N, M;
string s[MAXN];
struct SAM {
int ch[MAXN][26], fa[MAXN], len[MAXN], tot, las, root;
void init() {
for(int i = 0; i <= tot; i++)
fa[i] = 0, len[i] = 0, memset(ch[i], 0, sizeof(ch[i]));
tot = root = 1; las = 1;
}
void insert(int x) {
int now = ++tot, pre = las; las = now; len[now] = len[pre] + 1;
for(; pre && !ch[pre][x]; pre = fa[pre]) ch[pre][x] = now;
if(!pre) {fa[now] = root; return ;}
int q = ch[pre][x];
if(len[q] == len[pre] + 1) fa[now] = q;
else {
int nq = ++tot; fa[nq] = fa[q]; len[nq] = len[pre] + 1; fa[q] = fa[now] = nq;
memcpy(ch[nq], ch[q], sizeof(ch[q]));
for(; pre && ch[pre][x] == q; pre = fa[pre]) ch[pre][x] = nq;
}
}
void Build(string str) {
init();
for(auto &x: str)
insert(x - 'a');
}
int find(string str) {
int cur = 0, now = root;
for(auto &x : str) {
int v = x - 'a';
if(ch[now][v]) cur++, now = ch[now][v];
else return cur;
}
return cur;
}
}S[2];
void solve() {
cin >> N;
for(int i = 1; i <= N; i++) cin >> s[i];
cin >> M;
while(M--) {
int x, y;
cin >> x >> y;
S[0].Build(s[x]);
S[1].Build(s[y]);
int ans = 0;
for(int i = 1; i <= N; i++)
ans = max(ans, min(S[0].find(s[i]), S[1].find(s[i])));
cout << ans << '\n';
}
}
int main() {
// freopen("a.in", "r", stdin);
ios::sync_with_stdio(0);
int T; cin >> T;
for(; T--; solve());
return 0;
}
HDU 6138 Fleet of the Eternal Throne(后缀自动机)的更多相关文章
- HDU 6138 Fleet of the Eternal Throne 后缀数组 + 二分
Fleet of the Eternal Throne Problem Description > The Eternal Fleet was built many centuries ago ...
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne AC自动机或者KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...
- 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 ...
- HDU 6138 Fleet of the Eternal Throne(AC自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=6138 [题目大意] 给出一些串,询问第x个串和第y个串的公共子串, 同时要求该公共子串为某个串的前 ...
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne 思维,暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给了初始区间[-1,1],然后有一些操作,可以r加上一个数,l减掉一个数,或者同时操作,问 ...
- HDU 4416 Good Article Good sentence(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4416 [题目大意] 给出一个字符串,然后,给出一个字符串集合,问在该字符串中出现,且不在字符串集合 ...
- HDU 4622 Reincarnation(后缀自动机)
[题目链接] http://acm.hdu.edu.cn/showproblem.php?pid=4622 [题目大意] 给出一个长度不超过2000的字符串,有不超过10000个询问,问[L,R]子串 ...
- HDU 5442 后缀自动机(从环字符串选定一个位置 , 时针或顺时针走一遍,希望得到字典序最大)
http://acm.hdu.edu.cn/showproblem.php?pid=5442 题目大意: 给定一个字符串,可理解成环,然后选定一位置,逆时针或顺时针走一遍,希望得到字典序最大,如果同样 ...
- HDU 4436 (后缀自动机)
HDU 4436 str2int Problem : 给若干个数字串,询问这些串的所有本质不同的子串转换成数字之后的和. Solution : 首先将所有串丢进一个后缀自动机.由于这道题询问的是不同的 ...
随机推荐
- 788. Rotated Digits
X is a good number if after rotating each digit individually by 180 degrees, we get a valid number t ...
- flask源码解析之上下文
引入 对于flask而言,其请求过程与django有着截然不同的流程.在django中是将请求一步步封装最终传入视图函数的参数中,但是在flask中,视图函数中并没有请求参数,而是将请求通过上下文机制 ...
- 《http权威指南》读书笔记1
概述 最近对http很感兴趣,于是开始看<http权威指南>.别人都说这本书有点老了,而且内容太多.我个人觉得这本书写的太好了,非常长知识,让你知道关于http的很多概念,不仅告诉你怎么做 ...
- asp.net mvc 安全测试漏洞 " HTTP 动词篡改的认证旁路" 问题解决
IBM Security Appscan漏洞筛查-HTTP 动词篡改的认证旁路漏洞,具体解决方案: 在Web.Config中system.webServer节点增加配置security: <se ...
- Linux - 创建定时任务
crontab命令 用来创建周期性定时任务 crontab {-l|-r|-e} -l 显示当前的 crontab -r 删除当前的 crontab -e 使用编辑器编辑当前 crontab 文件 输 ...
- Win10手记-为应用集成日志工具Logger
日志工具由来已久,是很受大家欢迎的debug工具.其中.NET平台上很出名的是log4net,但是由于Windows 10通用应用项目没有了System.Configuration引用,所以也就不能很 ...
- c++编程之内存的分配
当我们在进行编程时,特别是使用c++语言进行编程时,需要知道内存有几个内存区可供我们使用,因为c++可以直接操作内存.接下让我们来看看内存中的几大内存区. 1.栈区 栈区(stack)是速度最快的一个 ...
- 【译】使用 ndb 调试 node 应用
原文链接:Debugging Node.js Application Using ndb Google Chrome 实验室发布了一款新的 node debug 工具来提升开发者体验,本文将会全面介绍 ...
- C# 判断字符串是否符合十六进制,八进制,二进制和十进制整数格式的正则表达式
/// <summary> /// 判断是否十六进制格式字符串 /// </summary> /// <param name="str">< ...
- 使用Dockerfile创建支持SSH服务的镜像
1.前面我们学习了使用Dockerfile,那接下来我们就用Dockerfile创建一个支持SSH服务的镜像. 2.首先创建一个目录ssh_centos [root@rocketmq-nameserv ...