HDOJ 4691 Front compression 尾碼數組,hdoj4691

來源:互聯網
上載者:User

HDOJ 4691 Front compression 尾碼數組,hdoj4691


尾碼數組求兩子串間的最大公用首碼.

Front compressionTime Limit: 5000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others)
Total Submission(s): 1382    Accepted Submission(s): 517


Problem DescriptionFront compression is a type of delta encoding compression algorithm whereby common prefixes and their lengths are recorded so that they need not be duplicated. For example:

The size of the input is 43 bytes, while the size of the compressed output is 40. Here, every space and newline is also counted as 1 byte.
Given the input, each line of which is a substring of a long string, what are sizes of it and corresponding compressed output? 
InputThere are multiple test cases. Process to the End of File.
The first line of each test case is a long string S made up of lowercase letters, whose length doesn't exceed 100,000. The second line contains a integer 1 ≤ N ≤ 100,000, which is the number of lines in the input. Each of the following N lines contains two integers 0 ≤ A < B ≤ length(S), indicating that that line of the input is substring [A, B) of S. 
OutputFor each test case, output the sizes of the input and corresponding compressed output. 
Sample Input
frcode20 60 6unitedstatesofamerica30 60 120 21myxophytamyxopodnabnabbednabbingnabit60 99 1616 1919 2525 3232 37
 
Sample Output
14 1242 3143 40
 
AuthorZejun Wu (watashi) 
Source2013 Multi-University Training Contest 9 


#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <cmath>using namespace std;typedef long long int LL;const int maxn=102100;int sa[maxn],rank[maxn],rank2[maxn],h[maxn],c[maxn],*x,*y,ans[maxn];char str[maxn];bool cmp(int* r,int a,int b,int l,int n){    if(r[a]==r[b]&&a+l<n&&b+l<n&&r[a+l]==r[b+l]) return true;    return false;}void radix_sort(int n,int sz){    for(int i=0;i<sz;i++) c[i]=0;    for(int i=0;i<n;i++) c[x[y[i]]]++;    for(int i=1;i<sz;i++) c[i]+=c[i-1];    for(int i=n-1;i>=0;i--) sa[--c[x[y[i]]]]=y[i];}void get_sa(char c[],int n,int sz=128){    x=rank,y=rank2;    for(int i=0;i<n;i++) x[i]=c[i],y[i]=i;    radix_sort(n,sz);    for(int len=1;len<n;len<<=1)    {        int yid=0;        for(int i=n-len;i<n;i++) y[yid++]=i;        for(int i=0;i<n;i++)            if(sa[i]>=len) y[yid++]=sa[i]-len;        radix_sort(n,sz);        swap(x,y);        x[sa[0]]=yid=0;        for(int i=1;i<n;i++)        {            x[sa[i]]=cmp(y,sa[i],sa[i-1],len,n)?yid:++yid;        }        sz=yid+1;        if(sz>=n) break;    }    for(int i=0;i<n;i++)        rank[i]=x[i];}void get_h(char str[],int n){    int k=0; h[0]=0x3f3f3f3f;    for(int i=0;i<n;i++)    {        if(rank[i]==0) continue;        k=max(k-1,0);        int j=sa[rank[i]-1];        while(i+k<n&&j+k<n&&str[i+k]==str[j+k]) k++;        h[rank[i]]=k;    }}int dp[maxn][20],Log[maxn];void RMQ_init(int n){    for(int i=0;i<n;i++) dp[i][0]=h[i];    for(int i=1;i<=Log[n];i++)    {        for(int j=0;j+(1<<i)-1<n;j++)        {            dp[j][i]=min(dp[j][i-1],dp[j+(1<<(i-1))][i-1]);        }    }}int lcp(int l,int r){    l=rank[l];r=rank[r];    if(l>r) swap(l,r);    ///!!!!!if(l==r) return n-sa[l];    int a=l+1,b=r;    int k=Log[b-a+1];    return min(dp[a][k],dp[b-(1<<k)+1][k]);}void init_Log(){    Log[0]=-1;    for(int i=1;i<maxn;i++)    {        Log[i]=(i&(i-1))?Log[i-1]:Log[i-1]+1;    }}void debug(int n){    for(int i=0;i<n;i++)    {        cout<<sa[i]<<",";    }    cout<<endl;    for(int j=0;j<n;j++)    {        cout<<h[j]<<",";    }    cout<<endl;    for(int j=0;j<n;j++)    {        cout<<rank[j]<<",";    }    cout<<endl;    int a,b;    while(cin>>a>>b)    cout<<"lcp: "<<lcp(a,b)<<endl;}int getwei(int x){    if(x==0) return 1;    int ret=0;    while(x)    {        x/=10;        ret++;    }    return ret;}int main(){    init_Log();    while(scanf("%s",&str)!=EOF)    {        int n=strlen(str);        get_sa(str,n);        get_h(str,n);        RMQ_init(n);        //debug(n);        int m;        LL ans1=0,ans2=0;        scanf("%d",&m);        bool first=true;        int lastL,lastR;        while(m--)        {            int l,r;            scanf("%d%d",&l,&r);            ans1+=r-l+1LL;            if(first==true)            {                ans2+=r-l+3;                first=false;            }            else            {                if(lastL==l)                {                    int minR=min(lastR-1,r-1);                    int com=minR-l+1;///公用長度                    int diff=r-l+1-com;///不同的長度                    int wei=getwei(com);                    ans2+=diff+wei+1LL;                    if(diff==0) ans2+=1LL;                }                else                {                    int com=lcp(lastL,l);///LCP                    com=min(com,min(lastR-lastL,r-l));///公用長度                    int diff=r-l+1-com;                    int wei=getwei(com);                    ans2+=diff+wei+1LL;                    if(diff==0) ans2+=1LL;                }            }            lastL=l; lastR=r;        }        cout<<ans1<<" "<<ans2<<endl;    }    return 0;}






聯繫我們

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