【HDU 5030】Rabbit's String (二分+后缀数组)

Rabbit's String

 

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".
 
 
【题意】
  给出一个字符串,你最多将他分成K个子串,在每个子串中挑出字典序最大的子串,在从挑出的所有字符串中挑出字典序最大的字符串。现在希望,最后挑出的字符串足够小。
 
【分析】
 
  首先,这题具有单调性,而且是求最大串最小,所以我们可以二分答案串。
  怎么二分答案串呢,我们不是已经用后缀数组求出了sa数组吗,sa数组表示的串是排过序的,其中每个后缀的前缀子串大小按长度的递增而递增,所以可以在sa数组里面二分。(我是先二分后缀,再二分长度)
  然后是判断,怎么判断是不是可以划分成至多k个串使他们都不超过二分串。
  还是在sa上做。
  如果他的sa位置小于mid,那么不用管,因为它怎么样都是小于二分串的。
  如果他的sa位置大于等于mid,而且他跟二分串没有LCP,那么这个二分一定没有答案,因为最小二分都使他不符合。
  除此之外,求出sa位置大于等于mid的所有串跟二分串的LCP,在sa[i]~sa[i]+lcp-1的位置上一定要至少打一个标记,因为不打标记它就会比二分串大了。
  
  所以最后我们会得到很多个区间,在这些区间里面至多打k-1个标记,使得每个区间中有含有一个标记。
 
  转化成了这样,就很容易做了。貌似是smg区间覆盖之类的问题。排个序,去个重,判断+累加一下就可以了。
 
代码如下:
  1 #include<cstdio>
  2 #include<cstdlib>
  3 #include<cstring>
  4 #include<iostream>
  5 #include<algorithm>
  6 #include<queue>
  7 using namespace std;
  8 #define Maxn 100010
  9 #define INF 0xfffffff
 10 
 11 char s[Maxn];
 12 int l,c[Maxn],cl,k;
 13 
 14 void init()
 15 {
 16     scanf("%s",s);
 17     l=strlen(s);cl=0;
 18     for(int i=0;i<l;i++) c[++cl]=s[i]-'a'+1;
 19 }
 20 
 21 int mymin(int x,int y) {return x<y?x:y;}
 22 
 23 int rk[Maxn],sa[Maxn],Rs[Maxn],y[Maxn],wr[Maxn];
 24 void get_sa(int m)
 25 {
 26     memcpy(rk,c,sizeof(rk));
 27     for(int i=0;i<=m;i++) Rs[i]=0;
 28     for(int i=1;i<=cl;i++) Rs[rk[i]]++;
 29     for(int i=1;i<=m;i++) Rs[i]+=Rs[i-1];
 30     for(int i=cl;i>=1;i--) sa[Rs[rk[i]]--]=i;
 31     
 32     int p=0,ln=1;
 33     while(p<cl)
 34     {
 35         int kk=0;
 36         for(int i=cl-ln+1;i<=cl;i++) y[++kk]=i;
 37         for(int i=1;i<=cl;i++) if(sa[i]>ln) y[++kk]=sa[i]-ln;
 38         for(int i=1;i<=cl;i++) wr[i]=rk[y[i]];
 39         
 40         for(int i=0;i<=m;i++) Rs[i]=0;
 41         for(int i=1;i<=cl;i++) Rs[wr[i]]++;
 42         for(int i=1;i<=m;i++) Rs[i]+=Rs[i-1];
 43         for(int i=cl;i>=1;i--) sa[Rs[wr[i]]--]=y[i];
 44         
 45         for(int i=1;i<=cl;i++) wr[i]=rk[i];
 46         for(int i=cl+1;i<=cl+ln;i++) wr[i]=0;
 47         p=1,rk[sa[1]]=1;
 48         for(int i=2;i<=cl;i++)
 49         {
 50             if(wr[sa[i]]!=wr[sa[i-1]]||wr[sa[i]+ln]!=wr[sa[i-1]+ln]) p++;
 51             rk[sa[i]]=p;
 52         }
 53         m=p,ln*=2;
 54     }
 55     sa[0]=rk[0]=0;
 56 }
 57 
 58 int height[Maxn];
 59 void get_he()
 60 {
 61     int kk=0;
 62     for(int i=1;i<=cl;i++) if(rk[i]!=1)
 63     {
 64         int j=sa[rk[i]-1];
 65         if(kk) kk--;
 66         while(c[i+kk]==c[j+kk]&&i+kk<=cl&&j+kk<=cl) kk++;
 67         height[rk[i]]=kk;
 68     }
 69     height[1]=0;
 70 }
 71 
 72 struct hp
 73 {
 74     int x,y;
 75 }a[Maxn];int al;
 76 
 77 bool cmp(hp x,hp y) {return (x.y==y.y)?(x.x>y.x):(x.y<y.y);}
 78 
 79 bool check(int x,int l)
 80 {
 81     al=0;int minn=l;
 82     if(l!=cl-sa[x]+1) a[++al].x=sa[x],a[al].y=sa[x]+l-1;
 83     for(int i=x+1;i<=cl;i++)
 84     {
 85         if(height[i]==0) return 0;
 86         minn=mymin(minn,height[i]);
 87         a[++al].x=sa[i],a[al].y=sa[i]+minn-1;
 88     }
 89     sort(a+1,a+1+al,cmp);
 90     int p=0;
 91     if(al>0) p=1;
 92     for(int i=2;i<=al;i++)
 93     {
 94         if(a[i].x>a[p].x) a[++p]=a[i];
 95     }
 96     int mx=0,cnt=0;
 97     for(int i=1;i<=p;i++)
 98     {
 99         if(mx<a[i].x) mx=a[i].y,cnt++;
100     }
101     return cnt<k;
102 }
103 
104 int fffind(int x)
105 {
106     int l,r;bool ok=0;
107     l=(x==1)?1:height[x]+1;
108     r=cl-sa[x]+1;
109     while(l<r)
110     {
111         int mid=(l+r)>>1;
112         if(check(x,mid)) r=mid,ok=1;
113         else l=mid+1;
114     }
115     if(check(x,l)) ok=1;
116     if(!ok) return -1;
117     return l;
118 }
119 
120 void ffind()
121 {
122     int l=1,r=cl;
123     while(l<r)
124     {
125         int mid=(l+r)>>1;
126         if(fffind(mid)!=-1) r=mid;
127         else l=mid+1;
128     }
129     int x=fffind(l);
130     for(int i=sa[l];i<=sa[l]+x-1;i++) printf("%c",c[i]-1+'a');
131     printf("\n");
132 }
133 
134 int main()
135 {
136     while(1)
137     {
138         scanf("%d",&k);
139         if(k==0) break;
140         init();
141         get_sa(30);
142         get_he();
143         ffind();
144     }
145     return 0;
146 }
[HDU5030]

 

2016-07-20 15:17:13

 
posted @ 2016-07-20 10:59  konjak魔芋  阅读(528)  评论(0编辑  收藏  举报