传送阵: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的更多相关文章

  1. 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 ...

  2. HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )

    题意:给定一个环形字符串,让他把它分成k份,使得最大的字典序 最小. 思路:二分答案,首先很明显答案所有可能是 n*n种  排序可以先求出最长公共前缀,这样比较就只需要比较公共前缀的下一位就能比较出两 ...

  3. 【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 ...

  4. 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 ...

  5. HDU - 5088: Revenge of Nim II (问是否存在子集的异或为0)

    Nim is a mathematical game of strategy in which two players take turns removing objects from distinc ...

  6. 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 ...

  7. 【HDU - 1029】Ignatius and the Princess IV (水题)

    Ignatius and the Princess IV  先搬中文 Descriptions:   给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...

  8. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  9. (数学)Knight's Trip -- hdu -- 3766

    http://acm.hdu.edu.cn/showproblem.php?pid=3766 Knight's Trip Time Limit: 2000/1000 MS (Java/Others)  ...

随机推荐

  1. 下拉列表 select-option ; select-optgroup-option

    HTML中的下拉列表: <select> <option value ="1">Volvo</option> <option value  ...

  2. destoon去掉会员注册email验证

    修改文件: /module/member/member.class.php 删除61行: //if(!is_email($email)) return $this->_($L['member_e ...

  3. C Primer Plus_第5章_运算符、表达式和语句_编程练习

    Practice 1. 输入分钟输出对应的小时和分钟. #include #define MIN_PER_H 60 int main(void) { int mins, hours, minutes; ...

  4. JavaScript高级程序设计学习笔记--错误处理与调试

    try-catch语句 只要代码中包含finally子句,则无论try或catch语句块中包含什么代码--甚至return语句,都不会阻止finally子句的执行,来看下面这个函数: function ...

  5. 解决weblogic.net.http.SOAPHttpsURLConnection incompatible with javax.net.ssl.HttpsURLConnection

    1. 按照网上的办法,可以修改代码解决问题,但是由于我们使用的是别人的jar包,不能修改代码,: URL url = new URL(null, "https://www.baidu.&qu ...

  6. mysql 分表

    1.分表,即把一个很大的表达数据分到几个表中,这样每个表数据都不多. 优点:提高并发量,减小锁的粒度 缺点:代码维护成本高,相关sql都需要改动 2.分区,所有的数据还在一个表中,但物理存储数据根据一 ...

  7. 从Eclipse迁移到Android Studio碰到的问题记录

    背景: 1. 一直在做.NET的开发,工作之余,学习了一下Android开发,写了一些Demo,当时用的Eclipse开发工具:这两天,刚好项目不是很忙,就打算把之前写的Demo,迁移到Android ...

  8. 解决Delphi图形化界面的TEdit、TLable等组件手动拖拽固定大小,但是编译之后显示有差别的情况

    经常遇到这样的情况,在我们使用Delphi的可视化工具进行UI设计的时候,我们拖拽TEdit或者Label组件,并且在可视化界面上设置它们的长.宽 但是当我们编译和运行程序的时候,却发现真正显示出来的 ...

  9. Pyqt 获取动态生成的QLineEdit值

    动态生成控件参考上一篇: http://www.cnblogs.com/dcb3688/p/4588814.html 那如何获取动态生成控件的值呢? 比如,动态的生成10个输入框QLineEdit,输 ...

  10. 【Java EE 学习 22 下】【单线程下载】【单线程断点下载】【多线程下载】

    一.文件下载简述 1.使用浏览器从网页上下载文件,Servlet需要增加一些响应头信息 (1)response.setContentType("application/force-downl ...