hdu 5030 Rabbit's String(后缀数组&二分法)
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
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.
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.
3
bbaa
2
ababa
0
b
baHintFor 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".
pid=5049" target="_blank">5049
你最多能够把它切成k个连续的子串。然后对于切出来的子串拿出他们子串字典序最大的那个(子串的子串)。
然后把全部拿出来的子串的子串字典序最大的那个串叫魔法串。
如今要你输出字典序最小的魔法串。
#include<algorithm>
#include<iostream>
#include<string.h>
#include<stdio.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
typedef long long ll;
char txt[maxn];
int sa[maxn],T1[maxn],T2[maxn],ct[maxn],he[maxn],rk[maxn],n,m,cut;
int mk[maxn];
ll f[maxn],ans;
void getsa(char *st)
{
int i,k,p,*x=T1,*y=T2;
for(i=0; i<m; i++) ct[i]=0;
for(i=0; i<n; i++) ct[x[i]=st[i]]++;
for(i=1; i<m; i++) ct[i]+=ct[i-1];
for(i=n-1; i>=0; i--)
sa[--ct[x[i]]]=i;
for(k=1,p=1; p<n; k<<=1,m=p)
{
for(p=0,i=n-k; i<n; i++) y[p++]=i;
for(i=0; i<n; i++) if(sa[i]>=k) y[p++]=sa[i]-k;
for(i=0; i<m; i++) ct[i]=0;
for(i=0; i<n; i++) ct[x[y[i]]]++;
for(i=1; i<m; i++) ct[i]+=ct[i-1];
for(i=n-1; i>=0; i--) sa[--ct[x[y[i]]]]=y[i];
for(swap(x,y),p=1,x[sa[0]]=0,i=1; i<n; i++)
x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
}
}
void gethe(char *st)
{
int i,j,k=0;
for(i=0;i<n;i++) rk[sa[i]]=i;
for(i=0;i<n-1;i++)
{
if(k) k--;
j=sa[rk[i]-1];
while(st[i+k]==st[j+k]) k++;
he[rk[i]]=k;
}
}
bool isok(ll p)
{
int pos,len,i,pp,cnt;
pos=lower_bound(f+1,f+1+n,p)-f;//定位sa
len=he[pos]+p-f[pos-1];//确定串长
for(i=0;i<n;i++)
mk[i]=-1;
if(n-sa[pos]>len)//看自己所属后缀是否要切
mk[sa[pos]]=sa[pos]+len-1;
for(i=pos+1;i<=n;i++)
{
if(he[i]==0)
return false;
len=min(len,he[i]);//lcp
mk[sa[i]]=sa[i]+len-1;//排序比pos大一定要切割。 }
pp=n,cnt=0;
for(i=0;i<n;i++)
{
if(mk[i]!=-1)//能不切先不切和后面的一起切。贪心的思想。
pp=min(pp,mk[i]);
if(pp==i)
{
cnt++;
if(cnt>cut)
return false;
pp=n;
}
}
return cnt<cut;//切cnt次就是cnt+1块。 }
int main()
{
int i,pos,len;
ll low,hi,mid; while(scanf("%d",&cut),cut)
{
scanf("%s",txt);
n=strlen(txt)+1;
m=128;
getsa(txt);
gethe(txt);
n--;
f[1]=n-sa[1];
for(i=2;i<=n;i++)
f[i]=f[i-1]+n-sa[i]-he[i];
low=1,hi=f[n],ans=1;
while(low<=hi)
{
mid=(low+hi)>>1;
if(isok(mid))
ans=mid,hi=mid-1;
else
low=mid+1;
}
pos=lower_bound(f+1,f+1+n,ans)-f;
len=he[pos]+ans-f[pos-1];
txt[sa[pos]+len]=0;
printf("%s\n",txt+sa[pos]);
}
return 0;
}
版权声明:本文博主原创文章,博客,未经同意不得转载。
hdu 5030 Rabbit's String(后缀数组&二分法)的更多相关文章
- hdu 3553 Just a String (后缀数组)
hdu 3553 Just a String (后缀数组) 题意:很简单,问一个字符串的第k大的子串是谁. 解题思路:后缀数组.先预处理一遍,把能算的都算出来.将后缀按sa排序,假如我们知道答案在那个 ...
- HDU 5030 Rabbit's String
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5030 题意:给出一个长度为n的串S,将S分成最多K个子串S1,S2,……Sk(k<=K).选出每 ...
- hdu 6194 沈阳网络赛--string string string(后缀数组)
题目链接 Problem Description Uncle Mao is a wonderful ACMER. One day he met an easy problem, but Uncle M ...
- HDU 6194 string string string (后缀数组)
题意:给定一个字符串,问你它有多少个子串恰好出现 k 次. 析:后缀数组,先把height 数组处理出来,然后每次取 k 个进行分析,假设取的是 i ~ i+k-1,那么就有重复的,一个是 i-1 ~ ...
- HDU 1403 Longest Common Substring(后缀数组,最长公共子串)
hdu题目 poj题目 参考了 罗穗骞的论文<后缀数组——处理字符串的有力工具> 题意:求两个序列的最长公共子串 思路:后缀数组经典题目之一(模版题) //后缀数组sa:将s的n个后缀从小 ...
- Hackerrank--Ashton and String(后缀数组)
题目链接 Ashton appeared for a job interview and is asked the following question. Arrange all the distin ...
- HDU 4777 Rabbit Kingdom(树状数组)
HDU 4777 Rabbit Kingdom 题目链接 题意:给定一些序列.每次询问一个区间,求出这个区间和其它数字都互质的数的个数 #include <cstdio> #include ...
- HDU - 6223 Infinite Fraction Path (倍增+后缀数组)
题意:给定一个长度为n(n<=150000)的字符串,每个下标i与(i*i+1)%n连边,求从任意下标出发走n步能走出的字典序最大的字符串. 把下标看成结点,由于每个结点有唯一的后继,因此形成的 ...
- POJ3729 Facer’s string 后缀数组
Fa ...
随机推荐
- C#的百度地图开发(三)依据坐标获取位置、商圈及周边信息
原文:C#的百度地图开发(三)依据坐标获取位置.商圈及周边信息 我们得到了百度坐标,现在依据这一坐标来获取相应的信息.下面是相应的代码 public class BaiduMap { /// < ...
- spring 普通类获取四大作用域request、applicationContext、session、page
几乎所有web应用容器都提供了四种类似Map的结构:application session request page,Jsp或者Servlet通过向着这四个对象放入数据,从而实现Jsp和Servlet ...
- Oracle连接池
原由:许多用户可能在查询相同的数据库以获取相同的数据.在这些情况下,可以通过使应用程序共享到数据源的连接来提高应用程序的性能.否则,让每个用户打开和关闭单独的连接的开销会对应用程序性能产生不利影响.这 ...
- UNIX网络编程卷1 server程序设计范式7 预先创建线程,以相互排斥锁上锁方式保护accept
本文为senlie原创.转载请保留此地址:http://blog.csdn.net/zhengsenlie 1.预先创建一个线程池.并让每一个线程各自调用 accept 2.用相互排斥锁代替让每一个线 ...
- Xamarin:制作并发布apk
原文:Xamarin:制作并发布apk 终于到了激动人心的时刻:要向真机发布apk了.流程如下: 1 制作release版的android应用安装包apk文件: 1.1 用VS2012中文版制作:记得 ...
- sgu 286. Ancient decoration(最小环覆盖)
给你一个n个点,每个点度为k(k为偶数)的无向图,问是否能将图中的n条边染色,使得每个点都拥有两条被染色的边.也就是说,是否存在拥有原图中n条边的子图,使得每个点的度为2?仔细想想,每个点的度为2,实 ...
- Android正在使用Handler实现信息发布机制(一)
上一篇文章,我们谈到了电话Handler的sendMessage方法,最后,我们将进入一个电话 sendMessageAtTime方法,例如下列: public boolean sendMessage ...
- coco2d-x 基于视口的地图设计
<pre name="code" class="plain"> 基于视口的地图设计 DionysosLai 2014-06-14 第三人称游戏,玩家 ...
- POJ培训计划2253_Frogger(最短/floyd)
解决报告 意甲冠军: 乞讨0至1所有最大的道路值的最小数量. 思维: floyd. #include <iostream> #include <cstdio> #include ...
- Drupal 7 模 .info 文件描述
Drupal使用.info文件以节省话题(theme)和模块(modules)基本数据(metadata). 编码:推荐使用UTF-8.这是一个没有BOM(Byte Order Mark). 下面是一 ...