传送阵: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. CPU的内部架构和工作原理 (转,相当不错)

    http://blog.chinaunix.net/uid-23069658-id-3563960.html 一直以来,总以为CPU内部真是如当年学习<计算机组成原理>时书上所介绍的那样, ...

  2. 【Alpha版本】冲刺-Day1

    队伍:606notconnected 会议时间:11月9日 会议总结 张斯巍(433) 今天安排:设计登陆界面背景,图标的大小规定 完成度:90% 明天计划:主界面图标的修改,侧边栏背景设计,个人信息 ...

  3. Java多态与反射

    多态通过分离做什么和怎么做,从另一个角度将接口与实现分离开来:通过多态来消除类型之间的耦合关系,在Java中,多态也叫动态绑定,后期绑定或运行时绑定,那么什么是方法绑定? 方法调用绑定: 将一个方法与 ...

  4. VMware中的Ubuntu网络设置

    网络配置: VMware安装后会有两个默认网卡,分别是VMnet8(192.168.83.1)和VMnet1(192.168.19.1),当然不同的机器上,这两个网卡的 IP会不同的.在windows ...

  5. [Linux]系统调用理解(4)

    这是本专栏中进程相关的系统调用的最后一篇,用2个实例演示了以往学习的内容.其一是Mini Shell,仿常用的Bash而做,但对其作了大大简化:其二是一个Daemon程序,可以使读者一窥服务器编程的端 ...

  6. python3安装

    yum install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel ...

  7. Power BI FAQ

    关于Power BI,最近一直想写点什么,但是也想不到写什么.直到前几天,有个朋友在qq加我,问了我好几个问题,我发现都是一些很有代表性的问题,所以都记录了下来,特意整理了一下分享出来,希望能帮到更多 ...

  8. 主动模式FTP与被动模式FTP该如何选择

    主动模式FTP与被动模式FTP该如何选择  无论是主动模式还是被动模式,其要进行文件传输都必须依次建立两个连接,分别为命令连接与数据连结.而主动模式与被动模式的差异主要体现在数据连结通道上.为了说明两 ...

  9. winform TreeView的一些用法以及异步加载

    今天,主要弄了一下对于树型控件的一些方法,以及异步加载.参考: http://www.cnblogs.com/greatverve/archive/2012/03/23/winform-treevie ...

  10. linux下将不同线程绑定到不同core和cpu上——pthread_setaffinity_np

    =============================================================== linux下的单进程多线程的程序,要实现每个线程平均分配到多核cpu,主 ...