Rabbit's String

Time Limit: 40000/20000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 288    Accepted Submission(s): 108

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
  1. 3
  2. bbaa
  3. 2
  4. ababa
  5. 0
 
Sample Output
  1. b
  2. ba
  3. Hint
  4. For the first test case, the king may divide the string into "b", "b" and "aa".
  5. For the second test case, the king may divide the string into "aba" and "ba".
  6.  
 
Source
 
Recommend
hujie   |   We have carefully selected several similar problems for you:  5053 5052 5051 5050 

pid=5049" target="_blank">5049 

 题意:
给你一个长度不超过1e5的串。

你最多能够把它切成k个连续的子串。然后对于切出来的子串拿出他们子串字典序最大的那个(子串的子串)。

然后把全部拿出来的子串的子串字典序最大的那个串叫魔法串。

如今要你输出字典序最小的魔法串。

思路:
最大中的最小。非常easy想到二分(如今都是条件反射了)。首先我们求出sa,height。然后就能够求出f[i]即排名前i的后缀中有多少个不同的子串。然后我们二分魔法串的排名。通过排名我们能够借助f数组定位魔法串所属后缀的排名pos和魔法串的长度len。如今要解决的是怎么推断方案是否可行了。对于排名大于pos的后缀x假设tp=lcp(pos,x)=0肯定无解。无论怎么切都没用。否则我们能够在[sa[x],sa[x]+tp-1]选个位置切一刀。对于pos后的后缀都标记一次。然后贪心来切。看最少切多少刀。然后就能够判定了。

具体见代码:
  1. #include<algorithm>
  2. #include<iostream>
  3. #include<string.h>
  4. #include<stdio.h>
  5. using namespace std;
  6. const int INF=0x3f3f3f3f;
  7. const int maxn=100010;
  8. typedef long long ll;
  9. char txt[maxn];
  10. int sa[maxn],T1[maxn],T2[maxn],ct[maxn],he[maxn],rk[maxn],n,m,cut;
  11. int mk[maxn];
  12. ll f[maxn],ans;
  13. void getsa(char *st)
  14. {
  15. int i,k,p,*x=T1,*y=T2;
  16. for(i=0; i<m; i++) ct[i]=0;
  17. for(i=0; i<n; i++) ct[x[i]=st[i]]++;
  18. for(i=1; i<m; i++) ct[i]+=ct[i-1];
  19. for(i=n-1; i>=0; i--)
  20. sa[--ct[x[i]]]=i;
  21. for(k=1,p=1; p<n; k<<=1,m=p)
  22. {
  23. for(p=0,i=n-k; i<n; i++) y[p++]=i;
  24. for(i=0; i<n; i++) if(sa[i]>=k) y[p++]=sa[i]-k;
  25. for(i=0; i<m; i++) ct[i]=0;
  26. for(i=0; i<n; i++) ct[x[y[i]]]++;
  27. for(i=1; i<m; i++) ct[i]+=ct[i-1];
  28. for(i=n-1; i>=0; i--) sa[--ct[x[y[i]]]]=y[i];
  29. for(swap(x,y),p=1,x[sa[0]]=0,i=1; i<n; i++)
  30. x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
  31. }
  32. }
  33. void gethe(char *st)
  34. {
  35. int i,j,k=0;
  36. for(i=0;i<n;i++) rk[sa[i]]=i;
  37. for(i=0;i<n-1;i++)
  38. {
  39. if(k) k--;
  40. j=sa[rk[i]-1];
  41. while(st[i+k]==st[j+k]) k++;
  42. he[rk[i]]=k;
  43. }
  44. }
  45. bool isok(ll p)
  46. {
  47. int pos,len,i,pp,cnt;
  48. pos=lower_bound(f+1,f+1+n,p)-f;//定位sa
  49. len=he[pos]+p-f[pos-1];//确定串长
  50. for(i=0;i<n;i++)
  51. mk[i]=-1;
  52. if(n-sa[pos]>len)//看自己所属后缀是否要切
  53. mk[sa[pos]]=sa[pos]+len-1;
  54. for(i=pos+1;i<=n;i++)
  55. {
  56. if(he[i]==0)
  57. return false;
  58. len=min(len,he[i]);//lcp
  59. mk[sa[i]]=sa[i]+len-1;//排序比pos大一定要切割。
  60.  
  61. }
  62. pp=n,cnt=0;
  63. for(i=0;i<n;i++)
  64. {
  65. if(mk[i]!=-1)//能不切先不切和后面的一起切。贪心的思想。
  66. pp=min(pp,mk[i]);
  67. if(pp==i)
  68. {
  69. cnt++;
  70. if(cnt>cut)
  71. return false;
  72. pp=n;
  73. }
  74. }
  75. return cnt<cut;//切cnt次就是cnt+1块。
  76.  
  77. }
  78. int main()
  79. {
  80. int i,pos,len;
  81. ll low,hi,mid;
  82.  
  83. while(scanf("%d",&cut),cut)
  84. {
  85. scanf("%s",txt);
  86. n=strlen(txt)+1;
  87. m=128;
  88. getsa(txt);
  89. gethe(txt);
  90. n--;
  91. f[1]=n-sa[1];
  92. for(i=2;i<=n;i++)
  93. f[i]=f[i-1]+n-sa[i]-he[i];
  94. low=1,hi=f[n],ans=1;
  95. while(low<=hi)
  96. {
  97. mid=(low+hi)>>1;
  98. if(isok(mid))
  99. ans=mid,hi=mid-1;
  100. else
  101. low=mid+1;
  102. }
  103. pos=lower_bound(f+1,f+1+n,ans)-f;
  104. len=he[pos]+ans-f[pos-1];
  105. txt[sa[pos]+len]=0;
  106. printf("%s\n",txt+sa[pos]);
  107. }
  108. return 0;
  109. }

版权声明:本文博主原创文章,博客,未经同意不得转载。

hdu 5030 Rabbit&#39;s String(后缀数组&amp;二分法)的更多相关文章

  1. hdu 3553 Just a String (后缀数组)

    hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...

  2. HDU 5030 Rabbit's String

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5030 题意:给出一个长度为n的串S,将S分成最多K个子串S1,S2,……Sk(k<=K).选出每 ...

  3. hdu 6194 沈阳网络赛--string string string(后缀数组)

    题目链接 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle M ...

  4. HDU 6194 string string string (后缀数组)

    题意:给定一个字符串,问你它有多少个子串恰好出现 k 次. 析:后缀数组,先把height 数组处理出来,然后每次取 k 个进行分析,假设取的是 i ~ i+k-1,那么就有重复的,一个是 i-1 ~ ...

  5. HDU 1403 Longest Common Substring(后缀数组,最长公共子串)

    hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...

  6. Hackerrank--Ashton and String(后缀数组)

    题目链接 Ashton appeared for a job interview and is asked the following question. Arrange all the distin ...

  7. HDU 4777 Rabbit Kingdom(树状数组)

    HDU 4777 Rabbit Kingdom 题目链接 题意:给定一些序列.每次询问一个区间,求出这个区间和其它数字都互质的数的个数 #include <cstdio> #include ...

  8. HDU - 6223 Infinite Fraction Path (倍增+后缀数组)

    题意:给定一个长度为n(n<=150000)的字符串,每个下标i与(i*i+1)%n连边,求从任意下标出发走n步能走出的字典序最大的字符串. 把下标看成结点,由于每个结点有唯一的后继,因此形成的 ...

  9. POJ3729 Facer’s string 后缀数组

                                                                                                      Fa ...

随机推荐

  1. Rational Rose的四种视图介绍

    Rose模型中有四种视图:Use Case View(用例视图),Logical View(逻辑视图),Component View(组建视图)和Deployment View(配置视图). 用例视图 ...

  2. WPF命中测试示例(一)——坐标点命中测试

    原文:WPF命中测试示例(一)--坐标点命中测试 命中测试也可被称为碰撞测试,在WPF中使用VisualTreeHelper.HitTest()方法实现,该方法用于获取给定的一个坐标点或几何形状内存在 ...

  3. Wake-On-LAN待机或休眠模式中唤醒

    Wake-On-LAN简称WOL,是一种电源管理功能:如果存在网络活动,则允许设备将操作系统从待机或休眠模式中唤醒.许多主板厂商支持IBM提出的网络唤醒标准.该标准允许网络管理员远程打开PC机电源,以 ...

  4. C# webservice初探

    转载请注明出处Coder的不平庸:http://blog.csdn.net/pearyangyang/article/details/46348633 因为工作的终端曾经是直接对数据库进行操作,导致每 ...

  5. Java集合类汇总记录-- apache.commons4(TreeList)

    通常.Tree是Tree,List是List,两者不太可能混在一起.但apache-commons库却用tree实现了实现了List的接口,也就是TreeList类.与标准的LinkedList相比. ...

  6. 构造Nexus,仓库部署成员Nexus仓

    在一个,我们描述了如何配置安装nexus制,本节,我们来介绍nexus采用 1.登录 在红色的部分点击登陆.输入username与password admin/admin123. 这里能够配置nexu ...

  7. [HTML5游戏开发]简单的《找没有同汉字版〗爆去考考您狄综力吧

    [color=ize:18px]一,筹办工做   本次 游戏开发需求用到lufylegend.js开源游戏引擎,版本我用的是1.5.2(如今最新的版本是1.6.0).    引擎下载的位置:http: ...

  8. Duanxx的C++学习: const指针具体解释

    Const指的是一个编译时的常量. keywordconst使得代码能够确定一个变量能否够被改动. 使用了const后,能够防止对变量或者指针的改动:更重要的是,const的引用能够防止对所引用的对象 ...

  9. Fitnesse用系列三

    动态决策表 动态决策表是新出,版本号到今年年初还没有了.我看了看文档和演示文稿样本,其效果是作为一种辅助通用决策表.它不是easy匹配的名称和发射.但假设只有一个或两个参数.不管名字怎么都找不到,这并 ...

  10. sql server从一个数据库复制一个表到另一个数据库的方法

    分两步进行: 第一步,复制表结构: 在表上面右击——>编写表脚本为:——>Create到——>新查询编辑器窗口,你也可以保存为sql文件, 将新查询编辑器窗口最上面的一句话USE [ ...