• 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.


    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 (xy), you should find the longest substring that have a risk of causing chain reaction.
     
    Input
    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.
     
    Output
    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
     

    题意:

      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+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(p=0,i=n-j;i<n;++i)y[p++]=i;
            for(i=0;i<n;++i)if(sa[i]>=j)y[p++]=sa[i]-j;
            for(i=0;i<m;++i)wm[i]=0;
            for(i=0;i<n;++i)wm[x[y[i]]]++;
            for(i=1;i<m;++i)wm[i]+=wm[i-1];
            for(i=n-1;i>=0;--i)sa[--wm[x[y[i]]]]=y[i];
            for(t=x,x=y,y=t,i=p=1,x[sa[0]]=0;i<n;++i) {
                x[sa[i]]=cmp(y,sa[i],sa[i-1],j)?p-1:p++;
            }
        }
        ran=x;
    }
    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]];
                i++;
            }
            if(vis[x] && vis[y] && fi) return 1;
        }
        return 0;
    }
    
    int m;
    string a[N];
    int main() {
        int T;
        scanf("%d",&T);
        while(T--) {
            scanf("%d",&n);
            memset(first,0,sizeof(first));
            memset(vis,0,sizeof(vis));
            int cnt = 0,san = 100;
            for(int i = 1; i <= n; ++i) {
                cin>>a[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;
            SA(r,sa,n+1,san+1);
            Height(r,sa,n);
            scanf("%d",&m);
            while(m--) {
                int x,y;
                scanf("%d%d",&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;
                }
                printf("%d
    ",ans);
            }
        }
        return 0;
    }
  • 相关阅读:
    富人和穷人的区别(转)
    asp.net主题的几种应用
    asp.net主题的几种应用
    富人和穷人的区别(转)
    SQL SERVER中一些常见性能问题的总结
    SQL SERVER中一些常见性能问题的总结
    Bind和Eval的区别详解
    优酷去广告代码
    使用jquery框架导致js功能失效解决办法
    absolute定位问题
  • 原文地址:https://www.cnblogs.com/zxhl/p/7382942.html
Copyright © 2020-2023  润新知