HDU 6138 Fleet of the Eternal Throne 后缀数组 + 二分
Fleet of the Eternal Throne
>
> — Wookieepedia
The major defeat of the Eternal Fleet is the connected defensive network. Though being effective in defensing a large fleet, it finally led to a chain-reaction and was destroyed by the Gravestone. Therefore, when the next generation of Eternal Fleet is built, you are asked to check the risk of the chain reaction.
The battleships of the Eternal Fleet are placed on a 2D plane of n rows. Each row is an array of battleships. The type of a battleship is denoted by an English lowercase alphabet. In other words, each row can be treated as a string. Below lists a possible configuration of the Eternal Fleet.
aa
bbbaaa
abbaababa
abba
If in the x-th row and the y-th row, there exists a consecutive segment of battleships that looks identical in both rows (i.e., a common substring of the x-th row and y-th row), at the same time the substring is a prefix of any other row (can be the x-th or the y-th row), the Eternal Fleet will have a risk of causing chain reaction.
Given a query (x, y), you should find the longest substring that have a risk of causing chain reaction.
For each test cases, the first line contains integer n (n≤105).
There are n lines following, each has a string consisting of lower case letters denoting the battleships in the row. The total length of the strings will not exceed 105.
And an integer m (1≤m≤100) is following, representing the number of queries.
For each of the following m lines, there are two integers x,y, denoting the query.
3
aaa
baaa
caaa
2
2 3
1 2
3
题意:
m只有100,
先吧所有字符串用一个没有出现过的 id 串起来
求一遍SA
我们对于每个询问x,y,去二分答案mid
对于后缀数组上 一段连续 的 值,如果最小值是大于 mid 的并且 包含了x,y串里的 字串, 并且还包含了 任意 给定母串的 前缀 ,那么就是可行的 Mid
上面的check可以O(n)
#include<bits/stdc++.h>
using namespace std;
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0); const int N = 6e5+,M=1e6+,inf=; int *ran,r[N],sa[N],height[N],wa[N],wb[N],wm[N];
bool cmp(int *r,int a,int b,int l) {
return r[a] == r[b] && r[a+l] == r[b+l];
}
void SA(int *r,int *sa,int n,int m) {
int *x=wa,*y=wb,*t;
for(int i=;i<m;++i)wm[i]=;
for(int i=;i<n;++i)wm[x[i]=r[i]]++;
for(int i=;i<m;++i)wm[i]+=wm[i-];
for(int i=n-;i>=;--i)sa[--wm[x[i]]]=i;
for(int i=,j=,p=;p<n;j=j*,m=p){
for(p=,i=n-j;i<n;++i)y[p++]=i;
for(i=;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
for(i=;i<m;++i)wm[i]=;
for(i=;i<n;++i)wm[x[y[i]]]++;
for(i=;i<m;++i)wm[i]+=wm[i-];
for(i=n-;i>=;--i)sa[--wm[x[y[i]]]]=y[i];
for(t=x,x=y,y=t,i=p=,x[sa[]]=;i<n;++i) {
x[sa[i]]=cmp(y,sa[i],sa[i-],j)?p-:p++;
}
}
ran=x;
}
void Height(int *r,int *sa,int n) {
for(int i=,j=,k=;i<n;height[ran[i++]]=k)
for(k?--k:,j=sa[ran[i]-];r[i+k] == r[j+k];++k);
} int vis[N],n,id[N],first[N];
int check(int x,int y,int len) {
int i = ,cnt = ,flag,fi = ;
while() {
while(i <= n && height[i] < len) i++;
if(i > n) return ;
vis[x] = ,vis[y] = ;fi = ; flag = id[sa[i-]];
fi = first[sa[i-]];
vis[flag] = ;
// if(len == 3) cout<<flag<<endl;
while(i <= n && height[i] >= len) {
vis[id[sa[i]]] = ;
// if(len == 3) cout<<id[sa[i]]<<endl;
fi |= first[sa[i]];
i++;
}
if(vis[x] && vis[y] && fi) return ;
}
return ;
} int m;
string a[N];
int main() {
int T;
scanf("%d",&T);
while(T--) {
scanf("%d",&n);
memset(first,,sizeof(first));
memset(vis,,sizeof(vis));
int cnt = ,san = ;
for(int i = ; i <= n; ++i) {
cin>>a[i];
int len = a[i].length();
for(int j = ; j < len; ++j) {
r[cnt++] = a[i][j] - 'a' + ;
id[cnt-] = i;
if(j == )
first[cnt-] = ;
}
id[cnt] = i;
r[cnt++] = san++;
}
r[--cnt] = ;
n = cnt;
SA(r,sa,n+,san+);
Height(r,sa,n);
scanf("%d",&m);
while(m--) {
int x,y;
scanf("%d%d",&x,&y);
int l = ,r = min(a[x].length(),a[y].length()),ans = ;
while(l <= r) {
int md = (l + r)>>;
if(check(x,y,md)) ans = md,l = md+;
else r = md - ;
}
printf("%d\n",ans);
}
}
return ;
}
HDU 6138 Fleet of the Eternal Throne 后缀数组 + 二分的更多相关文章
- HDU 6138 Fleet of the Eternal Throne(后缀自动机)
题意 题目链接 Sol 真是狗血,被疯狂卡常的原因竟是 我们考虑暴力枚举每个串的前缀,看他能在\(x, y\)的后缀自动机中走多少步,对两者取个min即可 复杂度\(O(T 10^5 M)\)(好假啊 ...
- 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 AC自动机或者KMP
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给n个串,每次询问x号串和y号串的最长公共子串的长度,这个子串必须是n个串中某个串的前缀 ...
- 2017多校第8场 HDU 6138 Fleet of the Eternal Throne 思维,暴力
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6138 题意:给了初始区间[-1,1],然后有一些操作,可以r加上一个数,l减掉一个数,或者同时操作,问 ...
- BZOJ 3230: 相似子串( RMQ + 后缀数组 + 二分 )
二分查找求出k大串, 然后正反做后缀数组, RMQ求LCP, 时间复杂度O(NlogN+logN) -------------------------------------------------- ...
- BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案
BZOJ_2946_[Poi2000]公共串_后缀数组+二分答案 Description 给出几个由小写字母构成的单词,求它们最长的公共子串的长度. 任务: l 读入单 ...
- 【bzoj4310】跳蚤 后缀数组+二分
题目描述 很久很久以前,森林里住着一群跳蚤.一天,跳蚤国王得到了一个神秘的字符串,它想进行研究. 首先,他会把串分成不超过 k 个子串,然后对于每个子串 S,他会从S的所有子串中选择字典序最大的那一个 ...
- BZOJ 1717 [USACO06DEC] Milk Patterns (后缀数组+二分)
题目大意:求可重叠的相同子串数量至少是K的子串最长长度 洛谷传送门 依然是后缀数组+二分,先用后缀数组处理出height 每次二分出一个长度x,然后去验证,在排序的后缀串集合里,有没有连续数量多于K个 ...
随机推荐
- 构建maven的web项目时注意的问题(出现Error configuring application listener of class org.springframework.web.context.ContextLoaderListener 或者前端控制器无法加载)
构建项目后或者导入项目后,我们需要bulid path--->config build path 特别是maven的依赖一定要 发布到WEB_INF的lib下面,不然在发布项目的时候,这些依赖都 ...
- hdu6061[NTT推公式] 2017多校3
/*hdu6061[NTT推公式] 2017多校3*/ #include <bits/stdc++.h> using namespace std; typedef long long LL ...
- BZOJ 3309 DZY Loves Math ——莫比乌斯反演
枚举$d=gcd(i,j)$ 然后大力反演 ——来自Popoqqq的博客. 然后大力讨论后面的函数的意义即可. http://blog.csdn.net/popoqqq/article/details ...
- Linux(12):期中架构(4)--- 前端部分:HTTP & Nginx & LNMP架构
HTTP协议概念原理说明 1. 当用户访问一个网站时经历的过程 # ①. 利用DNS服务,将输入的域名解析为相应的IP地址 a 本地主机输入域名后,会查询本地缓存信息和本地hosts b 本地主机会向 ...
- POJ2486 Apple Tree
Time Limit: 1000MS Memory Limit: 65536KB 64bit IO Format: %lld & %llu Description Wshxzt is ...
- 标准C程序设计七---05
Linux应用 编程深入 语言编程 标准C程序设计七---经典C11程序设计 以下内容为阅读: <标准C程序设计>(第7版) 作者 ...
- Laravel 表单及分页
控制器代码 //列表 public function index(){ //不带分页// $student = Student::get(); //带分页 $student = Student::pa ...
- 动态AdvStringGrid完美示例 (AdvStringGrid使用技巧/Cells)
http://www.cnblogs.com/JackSun/archive/2010/12/16/1908104.html 此窗体,只需要简单准备如下,即可运行: 1,添加一个TAdvStri ...
- Codeforces 515E Drazil and Park (ST表)
题目链接 Drazil and Park 中文题面 传送门 如果他选择了x和y,那么他消耗的能量为dx + dx + 1 + ... + dy - 1 + 2 * (hx + hy). 把这个式子写成 ...
- @OneToMany
实体关系之@OneToMany 博客分类: hibernate & ejb SQL Order.java package com.entity; import java.io.Seriali ...