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

Fleet of the Eternal Throne


Problem Description
> The Eternal Fleet was built many centuries ago before the time of Valkorion by an unknown race on the planet of Iokath. The fate of the Fleet's builders is unknown but their legacy would live on. Its first known action was in the annihilation of all life in Wild Space. It spread across Wild Space and conquered almost every inhabited world within the region, including Zakuul. They were finally defeated by a mysterious vessel known as the Gravestone, a massive alien warship that countered the Eternal Fleet's might. Outfitted with specialized weapons designed to take out multiple targets at once, the Gravestone destroyed whole sections of the fleet with a single shot. The Eternal Fleet was finally defeated over Zakuul, where it was deactivated and hidden away. The Gravestone landed in the swamps of Zakuul, where the crew scuttled it and hid it away.
> — 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.


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 (xy), you should find the longest substring that have a risk of causing chain reaction.
The first line of the input contains an integer  T, denoting the number of test cases. 

For each test cases, the first line contains integer n (n105).

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 (1m100) is following, representing the number of queries. 

For each of the following m lines, there are two integers x,y, denoting the query.
You should output the answers for the queries, one integer per line.


Sample Input
1 3 aaa baaa caaa 2 2 3 1 2


Sample Output
3 3



  先吧所有字符串用一个没有出现过的 id 串起来



  对于后缀数组上 一段连续 的 值,如果最小值是大于 mid 的并且 包含了x,y串里的 字串, 并且还包含了 任意 给定母串的 前缀 ,那么就是可行的 Mid


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+20,M=1e6+10,inf=2147483647;

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=0;i<m;++i)wm[i]=0;
    for(int i=0;i<n;++i)wm[x[i]=r[i]]++;
    for(int i=1;i<m;++i)wm[i]+=wm[i-1];
    for(int i=n-1;i>=0;--i)sa[--wm[x[i]]]=i;
    for(int i=0,j=1,p=0;p<n;j=j*2,m=p){
        for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i) {
void Height(int *r,int *sa,int n) {
    for(int i=0,j=0,k=0;i<n;height[ran[i++]]=k)
    for(k?--k:0,j=sa[ran[i]-1];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 = 2,cnt = 0,flag,fi = 0;
    while(1) {
        while(i <= n && height[i] < len) i++;
        if(i > n) return 0;
        vis[x] = 0,vis[y] = 0;fi = 0;

        flag = id[sa[i-1]];
        fi = first[sa[i-1]];
        vis[flag] = 1;
       // if(len == 3) cout<<flag<<endl;
        while(i <= n && height[i] >= len) {
            vis[id[sa[i]]] = 1;
            // if(len == 3) cout<<id[sa[i]]<<endl;
            fi |= first[sa[i]];
        if(vis[x] && vis[y] && fi) return 1;
    return 0;

int m;
string a[N];
int main() {
    int T;
    while(T--) {
        int cnt = 0,san = 100;
        for(int i = 1; i <= n; ++i) {
            int len = a[i].length();
            for(int j = 0; j < len; ++j) {
                r[cnt++] = a[i][j] - 'a' + 1;
                id[cnt-1] = i;
                if(j == 0)
                    first[cnt-1] = 1;
            id[cnt] = i;
            r[cnt++] = san++;
        r[--cnt] = 0;
        n = cnt;
        while(m--) {
            int x,y;
            int l = 1,r = min(a[x].length(),a[y].length()),ans = 0;
            while(l <= r) {
                int md = (l + r)>>1;
                if(check(x,y,md)) ans = md,l = md+1;
                else r = md - 1;
    return 0;



评论将由博主筛选后显示,对所有人可见 | 还能输入1000个字符 “速评一下”
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页