HDU 5030 Rabbit's string

Source: Internet
Author: User

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 5030

A string of N is given, which is divided into a maximum of K sub-strings S1, S2 ,...... SK (k <= K ). Select the largest SSI sub-string of each sub-string Si (1 <= I <= K. At last, the maximum value of k ssi is minimized.

Idea: first, use the suffix array to find all substrings. A binary answer string is used to determine whether a division method meets the requirements. For answer string a, set the suffix of the starting position of A to T, and cut the substring s [Li, Ri] In the suffix of [t + 1, N]. make RI <n (subscript 1 to n ), and the substring and a have a public prefix greater than 0 (if there is a suffix with a public prefix of 0, then a is not true ), what is the nature of such a substring? Their nature is that they add a string consisting of the next letter in the original string, which is larger than the answer string a (obviously. Because we are looking for a suffix after ). In theory, we have to intercept such a string because it cannot be associated with the next letter.

However, let's look at the following situations. For example, all such strings are: [], [], [], [], [], [], first, we need to cut it off at the position of 10, so that [] won't be tied together with the next letter, and then with this cut off, [], [], [6, 11], [6, 25] These strings are all divided into two segments, so they do not need to be cut off, and then cut off from the 40 position. The last two parts are divided into three parts.

 

const int INF=1000000005;const int N=111111;int r[N],sa[N],wa[N],wb[N],wd[N],rank[N],h[N];int cmp(int *r,int a,int b,int len){    return r[a]==r[b]&&r[a+len]==r[b+len];}void da(int *r,int *sa,int n,int m){    int i,j,p,*x=wa,*y=wb,*t;    FOR0(i,m) wd[i]=0;    FOR0(i,n) wd[x[i]=r[i]]++;    FOR1(i,m-1) wd[i]+=wd[i-1];for(i=n-1;i>=0;i--) sa[--wd[x[i]]]=i;    for(j=1,p=1;p<n;j<<=1,m=p)    {        p=0;for(i=n-j;i<=n-1;i++)y[p++]=i;        FOR0(i,n) if(sa[i]>=j) y[p++]=sa[i]-j;        FOR0(i,m) wd[i]=0;        FOR0(i,n) wd[x[i]]++;        FOR1(i,m-1) wd[i]+=wd[i-1];for(i=n-1;i>=0;i--) sa[--wd[x[y[i]]]]=y[i];        t=x;x=y;y=t;p=1;x[sa[0]]=0;        FOR1(i,n-1) x[sa[i]]=cmp(y,sa[i-1],sa[i],j)?p-1:p++;    }}void calHeight(int *r,int *sa,int n){    int i,j,k=0;    FOR1(i,n) rank[sa[i]]=i;    FOR0(i,n)    {        if(k) k--;        j=sa[rank[i]-1];        while(i+k<n&&j+k<n&&r[i+k]==r[j+k]) k++;        h[rank[i]]=k;    }}char s[N];int K;int n;i64 f[N];vector<pii > V,p;int cal(){if(SZ(V)==0) return 0;sort(all(V));int minR=INF;p.clear();int i;for(i=SZ(V)-1;i>=0;i--){if(minR<=V[i].second) continue;minR=V[i].second;p.pb(V[i]);}int ans=0,last=-1;sort(all(p));for(i=0;i<SZ(p);i++){if(last>=p[i].first) continue;ans++;last=p[i].second;}return ans;}int ansL,ansR;int ok(i64 M){int t=lower_bound(f+1,f+n+1,M)-f;int L=sa[t];int len=h[t]+M-f[t-1];ansL=L;ansR=L+len-1;V.clear();if(L+len<n) V.pb(MP(L,L+len-1));int i;for(i=t+1;i<=n;i++){len=min(len,h[i]);if(!len) return 0;if(sa[i]+len<n) V.pb(MP(sa[i],sa[i]+len-1));}return cal()<K;}void print(){int i;for(i=ansL;i<=ansR;i++) putchar(s[i]); puts("");}int main(){while(scanf("%d",&K)!=-1){if(!K) break;scanf("%s",s);n=strlen(s);int i;for(i=0;i<n;i++) r[i]=s[i]-‘a‘+1;r[n]=0;da(r,sa,n+1,30);calHeight(r,sa,n);for(i=1;i<=n;i++) f[i]=f[i-1]+n-sa[i]-h[i];i64 low=1,high=f[n],ans=f[n];while(low<=high){i64 M=(low+high)>>1;if(ok(M)) ans=min(ans,M),high=M-1;else low=M+1;}ok(ans);print();}} 

 

HDU 5030 Rabbit's string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.