Rabbit's String
Problem DescriptionLong long ago, there lived a lot of rabbits in the forest. One day, the king of the rabbit kingdom got a mysterious string and he wanted to study this string.At first, he would divide this string into no more than k substrings. Then for each substring S, he looked at all substrings of S, and selected the one which has the largest dictionary order. Among those substrings selected in the second round, the king then choose one which has the largest dictionary order, and name it as a "magic string".
Now he wanted to figure out how to divide the string so that the dictionary order of that "magic string" is as small as possible.
InputThere are at most 36 test cases.For each test case, the first line contains a integer k indicating the maximum number of substrings the king could divide, and the second line is the original mysterious string which consisted of only lower letters.
The length of the mysterious string is between 1 and 105 and k is between 1 and the length of the mysterious string, inclusive.
The input ends by k = 0.
OutputFor each test case, output the magic string.Sample Input3bbaa2ababaSample OutputbbaHintFor the first test case, the king may divide the string into "b", "b" and "aa".
For the second test case, the king may divide the string into "aba" and "ba".
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#define Maxn 100010
#define INF 0xfffffff char s[Maxn];
int l,c[Maxn],cl,k; void init()
{
scanf("%s",s);
l=strlen(s);cl=;
for(int i=;i<l;i++) c[++cl]=s[i]-'a'+;
} int mymin(int x,int y) {return x<y?x:y;} int rk[Maxn],sa[Maxn],Rs[Maxn],y[Maxn],wr[Maxn];
void get_sa(int m)
{
memcpy(rk,c,sizeof(rk));
for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[rk[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[rk[i]]--]=i; int p=,ln=;
while(p<cl)
{
int kk=;
for(int i=cl-ln+;i<=cl;i++) y[++kk]=i;
for(int i=;i<=cl;i++) if(sa[i]>ln) y[++kk]=sa[i]-ln;
for(int i=;i<=cl;i++) wr[i]=rk[y[i]]; for(int i=;i<=m;i++) Rs[i]=;
for(int i=;i<=cl;i++) Rs[wr[i]]++;
for(int i=;i<=m;i++) Rs[i]+=Rs[i-];
for(int i=cl;i>=;i--) sa[Rs[wr[i]]--]=y[i]; for(int i=;i<=cl;i++) wr[i]=rk[i];
for(int i=cl+;i<=cl+ln;i++) wr[i]=;
p=,rk[sa[]]=;
for(int i=;i<=cl;i++)
{
if(wr[sa[i]]!=wr[sa[i-]]||wr[sa[i]+ln]!=wr[sa[i-]+ln]) p++;
rk[sa[i]]=p;
}
m=p,ln*=;
}
sa[]=rk[]=;
} int height[Maxn];
void get_he()
{
int kk=;
for(int i=;i<=cl;i++) if(rk[i]!=)
{
int j=sa[rk[i]-];
if(kk) kk--;
while(c[i+kk]==c[j+kk]&&i+kk<=cl&&j+kk<=cl) kk++;
height[rk[i]]=kk;
}
height[]=;
} struct hp
{
int x,y;
}a[Maxn];int al; bool cmp(hp x,hp y) {return (x.y==y.y)?(x.x>y.x):(x.y<y.y);} bool check(int x,int l)
{
al=;int minn=l;
if(l!=cl-sa[x]+) a[++al].x=sa[x],a[al].y=sa[x]+l-;
for(int i=x+;i<=cl;i++)
{
if(height[i]==) return ;
minn=mymin(minn,height[i]);
a[++al].x=sa[i],a[al].y=sa[i]+minn-;
}
sort(a+,a++al,cmp);
int p=;
if(al>) p=;
for(int i=;i<=al;i++)
{
if(a[i].x>a[p].x) a[++p]=a[i];
}
int mx=,cnt=;
for(int i=;i<=p;i++)
{
if(mx<a[i].x) mx=a[i].y,cnt++;
}
return cnt<k;
} int fffind(int x)
{
int l,r;bool ok=;
l=(x==)?:height[x]+;
r=cl-sa[x]+;
while(l<r)
{
int mid=(l+r)>>;
if(check(x,mid)) r=mid,ok=;
else l=mid+;
}
if(check(x,l)) ok=;
if(!ok) return -;
return l;
} void ffind()
{
int l=,r=cl;
while(l<r)
{
int mid=(l+r)>>;
if(fffind(mid)!=-) r=mid;
else l=mid+;
}
int x=fffind(l);
for(int i=sa[l];i<=sa[l]+x-;i++) printf("%c",c[i]-+'a');
printf("\n");
} int main()
{
while()
{
scanf("%d",&k);
if(k==) break;
init();
get_sa();
get_he();
ffind();
}
return ;
}
[HDU5030]
2016-07-20 15:17:13