POJ 2217 Secretary (尾碼數組)

來源:互聯網
上載者:User

標籤:blog   2014   os   for   io   amp   

題目大意:

計算兩個字串的最長的公用字串字串的長度。


思路分析:
將兩個串合并起來。

然後直接跑尾碼數組求出height

然後就可以直接掃描一次height ,加個是不是在一個串中的判斷就可以了。


#include <cstdio>#include <iostream>#include <algorithm>#include <cstring>#define maxn 200005using namespace std;char str[maxn];int sa[maxn],t1[maxn],t2[maxn],c[maxn],n;void suffix(int m){    int *x=t1,*y=t2;    for(int i=0;i<m;i++)c[i]=0;    for(int i=0;i<n;i++)c[x[i]=str[i]]++;    for(int i=1;i<m;i++)c[i]+=c[i-1];    for(int i=n-1;i>=0;i--)sa[--c[x[i]]]=i;    for(int k=1;k<=n;k<<=1)    {        int p=0;        for(int i=n-k;i<n;i++)y[p++]=i;        for(int i=0;i<n;i++)if(sa[i]>=k)y[p++]=sa[i]-k;        for(int i=0;i<m;i++)c[i]=0;        for(int i=0;i<n;i++)c[x[y[i]]]++;        for(int i=0;i<m;i++)c[i]+=c[i-1];        for(int i=n-1;i>=0;i--)sa[--c[x[y[i]]]]=y[i];        swap(x,y);        p=1;x[sa[0]]=0;        for(int i=1;i<n;i++)        x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;        if(p>=n)break;        m=p;    }}int rank[maxn],height[maxn];void getheight(){    int k=0;    for(int i=0;i<n;i++)rank[sa[i]]=i;    for(int i=0;i<n;i++)    {        if(k)k--;        if(!rank[i])continue;        int j=sa[rank[i]-1];        while(str[i+k]==str[j+k])k++;        height[rank[i]]=k;    }}bool ok(int len){    int down,up;    down=up=sa[0];    for(int i=1;i<n;i++)    {        if(height[i]<len)        {            down=up=sa[i];        }        else        {            down=min(sa[i],down);            up=max(sa[i],up);        }        if(up-down>len)return true;    }    return false;}int bin(){    int l=4,r=n,ans=0;    while(l<=r)    {        int mid=(l+r)>>1;        if(ok(mid))        {            ans=mid,l=mid+1;        }        else r=mid-1;    }    return ans;}char tmp[11111];int main(){    int T;    scanf("%d",&T);    getchar();    while(T--)    {        gets(tmp);        int len=strlen(tmp);        int top=0;        for(int i=0;i<=len;i++)        str[top++]=tmp[i];        gets(tmp);        int cnt=0;        while(tmp[cnt]!='\0')str[top++]=tmp[cnt++];        n=top;        suffix(256);        getheight();        int ans=0;        for(int i=1;i<n;i++)        {            if((sa[i-1]<len)!=(sa[i]<len))            ans=max(ans,height[i]);        }        printf("Nejdelsi spolecny retezec ma delku %d.\n",ans);    }    return 0;}/*101 2 3 4 5 6 7 8 9 10*/


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.