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) ...
随机推荐
- 为Tcl编写C的扩展库
Tcl是一个比较简洁的脚本语言,官方地址 http://www.tcl.tk. tcl脚本加载C实现的动态库非常方便. 1. 为Tcl编写一个用C实现的扩展函数. #include <stdio ...
- mysql命令导出导入数据库
命令导出数据库: mysqldump -h[主机所在IP] -u[用户名] -p [要导出的数据库]>[导出的路径//[文件名].sql] 命令导入数据库: 1>首先,我们应该在cmd中进 ...
- Caffe学习系列(17): caffe源码分析 vector<Blob<Dtype>*>& bottom(转)
转自:http://blog.csdn.net/qq_14975217/article/details/51524042 Blob:4个维度 n x c x h x w: bottom[0] .bot ...
- linux Mint截图软件 Shutter
安装shutter: sudo add-apt-repository ppa:shutter/ppa sudo apt-get update sudo apt-get install shutter ...
- 百度地图用ip获取当前位置的经纬度(高精度)
步骤比较简单先上百度地图API官网,申请一个应用AK(访问凭据):查看一下高进度定位的API,看看是否都符合要求下面直接上代码 /** * 根据ip获取地理坐标 * @param ip * @retu ...
- java10
1:正则表达式(理解) (1)就是符合一定规则的字符串 (2)常见规则 A:字符 x 字符 x.举例:'a'表示字符a \\ 反斜线字符. \n 新行(换行)符 ('\u000A') \r 回车符 ( ...
- js对文本框特殊字符串过滤
HTML 中 onkeyup="stripscript(this)"; function stripscript(obj) { var s = $(obj).val(); var ...
- webservice发布服务:AXIS2及客户端调用
1.Axis2: 到官网下载axis2的压缩包. 解压后: 1.将lib文件下的jar包复制到项目中 2.在web-inf下创建services->META-INF->services.x ...
- Unity VR全景漫游
一.前言: 最近VR如火如茶,再不学习就落伍啦.有空闲时间,跟Rodolfo一起研究下相关知识. 本文介绍了两种方法来制作VR场景: 方法一:通过6张小图搭建的VR场景 方法二:通过一张全景图来搭建V ...
- execl表格VLOOKUP函数的使用
使用场景:最近在处理一个表格数据的时候出现了一点麻烦.想把另外表2里面对应的的数据放到表1里面,本来表2的ID是乱序的(这里为好看,就顺序排了.),一个个查找复制粘贴比较麻烦,后经大师指点VLOOKU ...