【HEOI2015】【BZOJ4032】最短不公用子串,heoi2015bzoj4032

來源:互聯網
上載者:User

【HEOI2015】【BZOJ4032】最短不公用子串,heoi2015bzoj4032

Description

在虐各種最長公用子串、子序列的題虐的不耐煩了之後,你決定反其道而行之。

一個串的“子串”指的是它的連續的一段,例如bcd是abcdef的子串,但bde不是。
一個串的“子序列”指的是它的可以不連續的一段,例如bde是abcdef的子串,但bdd不是。
下面,給兩個小寫字母串A,B,請你計算:
(1) A的一個最短的子串,它不是B的子串
(2) A的一個最短的子串,它不是B的子序列
(3) A的一個最短的子序列,它不是B的子串
(4) A的一個最短的子序列,它不是B的子序列
Input

有兩行,每行一個小寫字母組成的字串,分別代表A和B。

Output

輸出4行,每行一個整數,表示以上4個問題的答案的長度。如果沒有符合要求的答案,輸出-1.

Sample Input

aabbcc

abcabc
Sample Output

2

4

2

4
HINT

對於100%的資料,A和B的長度都不超過2000

我從未見過如此鬼畜的字串DP四合一.
自己只會做前兩問…果然SAM學的還是不到位…
最後膜了一發吉利跪吉利添動力

我還是想吐槽一句:
我自己的代碼真TM醜!

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MAXN 4100using namespace std;char A[MAXN],B[MAXN];int la,lb,ans;int F[MAXN];int f[MAXN>>1][MAXN>>1];//Question1 DP出尾碼兩兩間<del>SCP</del>LCP長度 int a[MAXN],change[MAXN>>1][MAXN>>1];//Question2 枚舉A串起始點貪心 struct sam//Question3 對B建尾碼自動機F i j表示A串中終止位置為i匹配到自動機中的第j個狀態的最短長度//Question 4 同樣對B建尾碼自動機+貪心 {    int p,q,np,nq,last,cnt;    int a[MAXN][26],len[MAXN],fa[MAXN];    sam()    {        last=++cnt;    }    void insert(int c)    {        p=last;np=last=++cnt;len[np]=len[p]+1;        while (!a[p][c]&&p) a[p][c]=np,p=fa[p];        if (!p) fa[np]=1;        else        {            q=a[p][c];            if (len[q]==len[p]+1)   fa[np]=q;            else            {                nq=++cnt;len[nq]=len[p]+1;                memcpy(a[nq],a[q],sizeof(a[q]));                fa[nq]=fa[q];fa[q]=fa[np]=nq;                while (a[p][c]==q)  a[p][c]=nq,p=fa[p];            }        }    }}sam;void print(){    if (ans==la+1)  puts("-1");    else    printf("%d\n",ans);}int main(){    scanf("%s",A+1);scanf("%s",B+1);A[0]='a';B[0]='a';    la=strlen(A+1);lb=strlen(B+1);ans=la+1;    for (int i=la;i;i--)        for (int j=lb;j;j--)            if (A[i]==B[j]) f[i][j]=f[i+1][j+1]+1;    for (int i=1;i<=la;i++)    {        int maxn=0;        for (int j=1;j<=lb;j++) maxn=max(maxn,f[i][j]);        if (maxn!=la-i+1)   ans=min(ans,maxn+1);        }    print();ans=la+1;    for (int i=0;i<26;i++)  a[i]=la+1;    for (int i=lb;i>=0;i--)    {        for (int j=0;j<26;j++)  change[i][j]=a[j];        a[B[i]-'a']=i;    }    for (int i=1;i<=la;i++)    {        int now=0;        for (int j=i;j<=la;j++)        {            now=change[now][A[j]-'a'];            if (now>la)            {                ans=min(ans,j-i+1);                break;            }        }    }    print();ans=la+1;int temp;    for (int i=1;i<=lb;i++) sam.insert(B[i]-'a');    memset(F,0x3f,sizeof(F));F[1]=0;    for (int i=1;i<=la;i++)        for (int j=1;j<=sam.cnt;j++)        {            if (!(temp=sam.a[j][A[i]-'a'])) ans=min(ans,F[j]+1);            else    F[temp]=min(F[temp],F[j]+1);        }    print();ans=la+1;    memset(F,0x3f,sizeof(F));F[0]=0;    for (int i=1;i<=la;i++)        for (int j=lb;j>=0;j--)        {            if ((temp=change[j][A[i]-'a'])>la)  ans=min(ans,F[j]+1);            else    F[temp]=min(F[temp],F[j]+1);        }    print();}

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.