【TJOI2015】【BZOJ3998】弦論,tjoi2015bzoj3998

來源:互聯網
上載者:User

【TJOI2015】【BZOJ3998】弦論,tjoi2015bzoj3998

Description

對於一個給定長度為N的字串,求它的第K小子串是什麼。

Input

第一行是一個僅由小寫英文字母構成的字串S

第二行為兩個整數T和K,T為0則表示不同位置的相同子串算作一個。T=1則表示不同位置的相同子串算作多個。K的意義如題所述。
Output

輸出僅一行,為一個數字串,為第K小的子串。如果子串數目不足K個,則輸出-1

Sample Input

aabc

0 3
Sample Output

aab

HINT

N<=5*10^5

T<2

K<=10^9
Source
字典序第k小子串
SAM裸題
張天揚論文有詳解
我這個傻逼求right集合的方法錯了一直WA…
第二天才調好QAQ

#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<algorithm>#define MAXN 1000010using namespace std;int T,n,k;char ch[MAXN>>1];int c[MAXN],od[MAXN];struct sam{    int p,q,np,nq,last,cnt;    int len[MAXN],a[MAXN][26],fa[MAXN];    int right[MAXN],sum[MAXN];    sam()    {        last=++cnt;    }    inline void insert(int c)    {        p=last;np=last=++cnt;len[np]=len[p]+1;len[np]=len[p]+1;right[np]=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];            }        }    }    inline void init()//求出right集合和可到達狀態總數sum     {        for (int i=1;i<=cnt;i++)    c[len[i]]++;        int L=strlen(ch);        for (int i=1;i<=L;i++)  c[i]+=c[i-1];        for (int i=cnt;i;i--)   od[c[len[i]]--]=i;        for (int i=cnt;i;i--)        {            int x=od[i];            if (T)  right[fa[x]]+=right[x];            else    right[x]=1;        }        right[1]=0;        for (int i=cnt;i;i--)        {            int x=od[i];            sum[x]=right[x];            for (int j=0;j<26;j++)  sum[x]+=sum[a[x][j]];        }    }    inline void dfs(int x,int nowk)    {        if (nowk<=right[x]) return;        nowk-=right[x];        for (int i=0;i<26;i++)            if (a[x][i])            {                if (nowk<=sum[a[x][i]])                {                    putchar(i+'a');                    dfs(a[x][i],nowk);                    return;                }                nowk-=sum[a[x][i]];            }    }}sam;int main(){    scanf("%s",ch);    scanf("%d%d",&T,&k);    int l=strlen(ch);    for (int i=0;i<l;i++)   sam.insert(ch[i]-'a');    sam.init();    if (k>sam.sum[1])   puts("-1");    else    sam.dfs(1,k);}

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

聯繫我們

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