• SPOJ220:Relevant Phrases of Annihilation(后缀数组)


    Description

    You are the King of Byteland. Your agents have just intercepted a batch of encrypted enemy messages concerning the date of the planned attack on your island. You immedietaly send for the Bytelandian Cryptographer, but he is currently busy eating popcorn and claims that he may only decrypt the most important part of the text (since the rest would be a waste of his time). You decide to select the fragment of the text which the enemy has strongly emphasised, evidently regarding it as the most important. So, you are looking for a fragment of text which appears in all the messages disjointly at least twice. Since you are not overfond of the cryptographer, try to make this fragment as long as possible.

    Input

    The first line of input contains a single positive integer t<=10, the number of test cases. t test cases follow. Each test case begins with integer n (n<=10), the number of messages. The next n lines contain the messages, consisting only of between 2 and 10000 characters 'a'-'z', possibly with some additional trailing white space which should be ignored.

    Output

    For each test case output the length of longest string which appears disjointly at least twice in all of the messages.

    Example

    Input:
    1
    4
    abbabba
    dabddkababa
    bacaba
    baba
    
    Output:
    2
    

    (in the example above, the longest substring which fulfills the requirements is 'ba')



    题意:给定n个字符串,求在每一个字符串中至少出现两次且不重叠的最长子串。

    思路:先将n个字符串连起来,中间用不同样的且没有 


    出如今字符串中的字符隔开。求后缀数组。然后二分答案。再将后缀分组。推断 


    的时候。要看是否有一组后缀在每一个原来的字符串中至少出现两次,而且在每一个 


    原来的字符串中,后缀的起始位置的最大值与最小值之差是否不小于当前答案 


    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <stack>
    #include <queue>
    #include <map>
    #include <set>
    #include <vector>
    #include <math.h>
    #include <bitset>
    #include <algorithm>
    #include <climits>
    using namespace std;
    
    #define LS 2*i
    #define RS 2*i+1
    #define UP(i,x,y) for(i=x;i<=y;i++)
    #define DOWN(i,x,y) for(i=x;i>=y;i--)
    #define MEM(a,x) memset(a,x,sizeof(a))
    #define W(a) while(a)
    #define gcd(a,b) __gcd(a,b)
    #define LL long long
    #define N 100005
    #define MOD 1000000007
    #define INF 0x3f3f3f3f
    #define EXP 1e-8
    int wa[N],wb[N],wsf[N],wv[N],sa[N];
    int rank[N],height[N],s[N],a[N];
    //sa:字典序中排第i位的起始位置在str中第sa[i]
    //rank:就是str第i个位置的后缀是在字典序排第几
    //height:字典序排i和i-1的后缀的最长公共前缀
    int cmp(int *r,int a,int b,int k)
    {
        return r[a]==r[b]&&r[a+k]==r[b+k];
    }
    void getsa(int *r,int *sa,int n,int m)//n要包括末尾加入的0
    {
        int i,j,p,*x=wa,*y=wb,*t;
        for(i=0; i<m; i++)  wsf[i]=0;
        for(i=0; i<n; i++)  wsf[x[i]=r[i]]++;
        for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
        for(i=n-1; i>=0; i--)  sa[--wsf[x[i]]]=i;
        p=1;
        j=1;
        for(; p<n; 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<n; i++)  wv[i]=x[y[i]];
            for(i=0; i<m; i++)  wsf[i]=0;
            for(i=0; i<n; i++)  wsf[wv[i]]++;
            for(i=1; i<m; i++)  wsf[i]+=wsf[i-1];
            for(i=n-1; i>=0; i--)  sa[--wsf[wv[i]]]=y[i];
            t=x;
            x=y;
            y=t;
            x[sa[0]]=0;
            for(p=1,i=1; i<n; i++)
                x[sa[i]]=cmp(y,sa[i-1],sa[i],j)? p-1:p++;
        }
    }
    void getheight(int *r,int n)//n不保存最后的0
    {
        int i,j,k=0;
        for(i=1; i<=n; i++)  rank[sa[i]]=i;
        for(i=0; i<n; i++)
        {
            if(k)
                k--;
            else
                k=0;
            j=sa[rank[i]-1];
            while(r[i+k]==r[j+k])
                k++;
            height[rank[i]]=k;
        }
    }
    
    char str[N];
    int id[N],maxn[N],minn[N];
    
    bool check(int mid,int n,int k)
    {
        int i,j;
        for(i = 0; i<=k; i++)
        {
            maxn[i] = 0;
            minn[i] = INF;
        }
        for(i = 1; i<=n; i++)
        {
            if(height[i]<mid)
            {
                for(j = 0; j<=k; j++)
                {
                    maxn[j] = 0;
                    minn[j] = INF;
                }
                maxn[id[sa[i]]] = sa[i];
                minn[id[sa[i]]] = sa[i];
            }
            else
            {
                //求出第id[sa[i]]个串中,所使用头和尾。差值必须大于等于二分的答案
                maxn[id[sa[i]]] = max(maxn[id[sa[i]]],sa[i]);
                minn[id[sa[i]]] = min(minn[id[sa[i]]],sa[i]);
                maxn[id[sa[i-1]]] = max(maxn[id[sa[i-1]]],sa[i-1]);
                minn[id[sa[i-1]]] = min(minn[id[sa[i-1]]],sa[i-1]);
                for(j = 0; j<k; j++)
                {
                    if(maxn[j]-minn[j]<mid)
                        break;
                }
                if(j==k) return true;
            }
        }
        return false;
    }
    
    int main()
    {
        int t,n,i,j,k;
        scanf("%d",&t);
        while(t--)
        {
            scanf("%d",&k);
            n = 0;
            for(i = 0; i<k; i++)
            {
                scanf("%s",str+n);
                for(; str[n]!=''; n++)
                {
                    s[n] = str[n];
                    id[n] = i;
                }
                s[n++] = '#'+i;
            }
            s[n-1] = 0;
            getsa(s,sa,n,255);
            getheight(s,n-1);
            int l = 0,r = 10000,mid,ans = 0;
            while(l<=r)
            {
                mid = (l+r)/2;
                if(check(mid,n,k))
                {
                    ans = mid;
                    l = mid+1;
                }
                else r = mid-1;
            }
            printf("%d
    ",ans);
        }
    
        return 0;
    }
    


  • 相关阅读:
    python中break与continue区别
    jmeter正则表达式
    python的input()函数与getpass标准库
    python实例练习-01登录
    python数据类型-字符串
    jmeter操作mysql
    jmeter文件下载
    字符逆序
    求解立方根
    求最小公倍数
  • 原文地址:https://www.cnblogs.com/cxchanpin/p/6923764.html
Copyright © 2020-2023  润新知