hdu 4898 The Revenge of the Princess’ Knight
传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4898
题目大意:一个首尾相连的字符串,将其分为k个子串,使得最大的字串最小
将所有子串排序,输出第k小即可
对于有循环节的串,用抽屉原理解决即可
#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
using namespace std;
struct node{
int l;
char ch[];
}s[];
char ch[];
int vis[];
int n,k;
bool operator < (node a,node b){
for(int i=;i<=n;++i)
if(a.ch[i]!=b.ch[i])return a.ch[i]<b.ch[i];
return a.ch[]<b.ch[];
}
void work2(){
for(int i=;i<=n;++i)ch[i+n]=ch[i];
for(int i=;i<=n;++i){
int k=i+n;s[i].l=i;
for(int j=i,l=;j<k;++j,++l){
s[i].ch[l]=ch[j];
}
}
sort(s+,s+n+);
memset(vis,,sizeof(vis));
for(int i=;i<k;++i){
int l=s[i].l;
vis[l]=;vis[l+n]=;
}
for(int i=s[k].l;!vis[i];++i)
putchar(ch[i]);
return;
}
int Next[];
void get_pre(){
char s[];
for(int i=;i<=n;++i)s[i]=ch[i],s[i+n]=s[i];
int i=,j=,k=;
for(;i<=n&&j<=n&&k<=n;){
if(s[i+k]==s[j+k])k++;
else if(s[i+k]>s[j+k])i=i+k+,k=;
else if(s[i+k]<s[j+k])j=j+k+,k=;
if(i==j)j++;
}
if(i>j)i=j;
for(k=;k<=n;++k)
ch[k]=s[i+k-];
}
bool check(){
Next[]=Next[]=;
for(int i=;i<=n;++i){
int j=Next[i-];
while(j&&ch[j+]!=ch[i])j=Next[j];
Next[i]=ch[j+]==ch[i]?j+:;
}
int l=n-Next[n];
if(n%l==)return ;
return ;
}
void work1(){
int l=n-Next[n];
int a=n/l;
if(a<k)work2();
else{
if(a%k==){
int p=a/k;
int len=l*p;
for(int i=;i<=len;++i)putchar(ch[i]);
return;
}else{
int p=a/k+;
int len=l*p;
for(int i=;i<=len;++i)putchar(ch[i]);
return;
}
}
}
void init(){
scanf("%d%d",&n,&k);
scanf("%s",ch+);
get_pre();
}
void work(){
if(check())work1();
else work2();
}
int T;
int main(){
scanf("%d",&T);
while(T--){
init();
work();
puts("");
}
return ;
}
hdu 4898 The Revenge of the Princess’ Knight的更多相关文章
- HDU 4898 The Revenge of the Princess’ Knight(后缀数组+二分+暴力)(2014 Multi-University Training Contest 4)
Problem Description There is an old country and the king fell in love with a devil. The devil always ...
- HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
题意:给定一个环形字符串,让他把它分成k份,使得最大的字典序 最小. 思路:二分答案,首先很明显答案所有可能是 n*n种 排序可以先求出最长公共前缀,这样比较就只需要比较公共前缀的下一位就能比较出两 ...
- 【HDU 4898】 The Revenge of the Princess’ Knight (后缀数组+二分+贪心+...)
The Revenge of the Princess’ Knight Problem Description There is an old country and the king fell in ...
- hdu 1026(Ignatius and the Princess I)BFS
Ignatius and the Princess I Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (J ...
- HDU - 5088: Revenge of Nim II (问是否存在子集的异或为0)
Nim is a mathematical game of strategy in which two players take turns removing objects from distinc ...
- hdu 1028 Sample Ignatius and the Princess III (母函数)
Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...
- 【HDU - 1029】Ignatius and the Princess IV (水题)
Ignatius and the Princess IV 先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- (数学)Knight's Trip -- hdu -- 3766
http://acm.hdu.edu.cn/showproblem.php?pid=3766 Knight's Trip Time Limit: 2000/1000 MS (Java/Others) ...
随机推荐
- php glob()函数实现目录文件遍历与寻找与模式匹配的文件路径
采用PHP函数glob实现寻找与模式匹配的文件路径,主要讨论glob()函数的作用和用法,利用glob函数读取目录比其它的要快N倍,因为glob函数是内置函数处理起来自然要快. 一,函数原型 arra ...
- mysql查询语句select-子查询
1 子查询定义 在一个表表达中可以调用另一个表表达式,这个被调用的表表达式叫做子查询(subquery),我么也称作子选择(subselect)或内嵌选择(inner select).子查询的结果传递 ...
- xmind portable
portable : http://dl2.xmind.cn/xmind-7.5-update1-portable.zip
- md5 (c语言)
/** * \file md5.h * * \brief MD5 message digest algorithm (hash function) * * Copyright (C) 2006-201 ...
- AFNetworking3.0介绍,收藏
参考:http://www.jianshu.com/p/5969bbb4af9f 很多时候,AFNetworking都是目前iOS开发者网络库中的不二选择.Github上2W+的star数足见其流行程 ...
- CSS初始化样式
为什么要初始化CSS? CSS初始化是指重设浏览器的样式.不同的浏览器默认的样式可能不尽相同,所以开发时的第一件事可能就是如何把它们统一.如果没对CSS初始化往往会出现浏览器之间的页面差异.每次新开发 ...
- MySQL 编译安装并且开启DEBUG模式
因为想分析下mysql中一些操作的内部执行过程,单纯的看源码太枯燥了,所以决定结合mysql的执行过程来分析,mysql作为一款成熟的数据库软件,在设计的时候就考虑到了调试的问题,只是想开启调试模式的 ...
- SQL IN ANY ,(all any) 区别
EXITS 和 IN 的区别: 从效率来看: 1) select * from T1 where exists(select 1 from T2 where T1.a=T2.a) ; T1数据量小而T ...
- 如何使用Android JetPlayer类
在Android中,还提供了对Jet播放的支持,Jet是由OHA联盟成员SONiVOX开发的一个交互音乐引擎.其包括两部分:JET播放器和JET引擎.JET常用于控制游戏的声音特效,采用MIDI(Mu ...
- post NSURLConnection请求网络数据
#import "ViewController.h" @interface ViewController () @end @implementation ViewControlle ...