http://www.elijahqi.win/archives/3234
Problem Description
Long 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.
Input
There 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.
Output
For each test case, output the magic string.
Sample Input
3 bbaa 2 ababa 0
Sample Output
b ba
Hint
For 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”.
Source
2014 ACM/ICPC Asia Regional Guangzhou Online
Recommend
hujie | We have carefully selected several similar problems for you: 6275 6274 6273 6272 6271
题解:https://blog.youkuaiyun.com/elijahqi/article/details/80098043
#include<cstdio>
#include<cstring>
#include<algorithm>
#define ll long long
using namespace std;
const int N=1e5+10;
ll sum;int K,mn[N][17],Log[N],rk[N<<1],rk1[N<<1],ansl,ansr;
int cnt[N],height[N],tmp[N],sa[N],n;char s[N];
inline int lcp(int x,int y){
if (x==y) return n-x+1;
x=rk[x];y=rk[y];if (x>y) swap(x,y);x+=1;//printf("%d %d\n",x,y);
int t=Log[y-x+1];//printf("%d\n",t);
return min(mn[x][t],mn[y-(1<<t)+1][t]);
}
inline void init(){
Log[0]=-1;memset(mn,0,sizeof(mn));
for (int i=1;i<=n;++i) Log[i]=Log[i>>1]+1;
for (int i=1;i<=n;++i) mn[i][0]=height[i];
for (int j=1;j<=Log[n];++j)
for (int i=1;i+(1<<j)-1<=n;++i)
mn[i][j]=min(mn[i][j-1],mn[i+(1<<j-1)][j-1]);
}
inline void get_k(ll k){
static ll now;now=0;
for (int i=1;i<=n;++i){
now=n-sa[i]-height[i]+1;
if (now<k) {k-=now;continue;}
ansl=sa[i];ansr=sa[i]+k+height[i]-1;break;
}
}
inline bool cmp(int l1,int r1,int l2,int r2){//s[l1,r1]<=s[l2,r2]->1
static int len1,len2,lp;len1=r1-l1+1;len2=r2-l2+1;lp=lcp(l1,l2);
if (lp>=len2&&len1>len2) return 0;
if (lp>=len1&&len2>=len1) return 1;
if (lp>=len1&&lp>=len2) return len1<=len2;
return s[l1+lp]<=s[l2+lp];
}
inline bool check(){static int lst,tim;lst=n;tim=1;
for (int i=n;i;--i){
if (s[i]>s[ansl]) return 0;
if (!cmp(i,lst,ansl,ansr)) lst=i,++tim;
if (tim>K) return 0;
}return 1;
}
int main(){
freopen("hdu5030.in","r",stdin);
while(~scanf("%d",&K)){if (!K) return 0;
scanf("%d",&K);scanf("%s",s+1);n=strlen(s+1);
memset(cnt,0,sizeof(cnt));memset(rk1,0,sizeof(rk1));memset(rk,0,sizeof(rk));
for (int i=1;i<=n;++i) cnt[s[i]]=1;int m=300;
for (int i=1;i<=m;++i) cnt[i]+=cnt[i-1];
for (int i=1;i<=n;++i) rk[i]=cnt[s[i]];int k=0;
for (int p=1;k!=n;p<<=1,m=k){
for (int i=1;i<=m;++i) cnt[i]=0;
for (int i=1;i<=n;++i) ++cnt[rk[i+p]];
for (int i=1;i<=m;++i) cnt[i]+=cnt[i-1];
for (int i=n;i;--i) tmp[cnt[rk[i+p]]--]=i;
for (int i=1;i<=m;++i) cnt[i]=0;
for (int i=1;i<=n;++i) ++cnt[rk[i]];
for (int i=1;i<=m;++i) cnt[i]+=cnt[i-1];
for (int i=n;i;--i) sa[cnt[rk[tmp[i]]]--]=tmp[i];
memcpy(rk1,rk,sizeof(rk)>>1);k=rk[sa[1]]=1;
for (int i=2;i<=n;++i){
if (rk1[sa[i]]!=rk1[sa[i-1]]||rk1[sa[i-1]+p]!=rk1[sa[i]+p]) ++k;
rk[sa[i]]=k;
}
}k=0;
for (int i=1;i<=n;++i){
if (rk[i]==1) continue;k=k==0?0:k-1;
while(s[i+k]==s[sa[rk[i]-1]+k]) ++k;
height[rk[i]]=k;
}init();sum=0;for (int i=1;i<=n;++i) sum+=i,sum-=height[i];
/* for (int i=1;i<=n;++i){
for (int j=sa[i];j<=n;++j) putchar(s[j]);puts("");
}*/
// for (int i=1;i<=n;++i) printf("%d\n",height[i]);
ll l=1,r=sum,mid;int L,R;
while(l<=r){
get_k(mid=l+r>>1);
if (check()) L=ansl,R=ansr,r=mid-1;else l=mid+1;
}for (int i=L;i<=R;++i) putchar(s[i]);puts("");
}
return 0;
}