传送阵: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. Spring中集合类型属性注入

    我们都知道如何去注入普通属性的值,非常简单,那么我们如何去注入开发中常见的集合类型的属性了,别急,往下看. 这里将介绍如何给Map list set Array Properties 这些属性注入值. ...

  2. 【Alpha版本】 第一天 11.7

    一.站立式会议照片: 二.项目燃尽图: 三.项目进展: 成 员 前段时间完成任务 今天完成任务 明天要做任务 问题困难 心得体会 胡泽善 部分APP功能 我要招聘详情的展示 注册界面的实现 一些特殊效 ...

  3. C和指针 第九章 习题

    9.15 编写函数格式化金钱为标准字符串 #include <stdio.h> #include <string.h> #define TEMP_LEN 1000 void d ...

  4. javascript数据结构-链表

    gihtub博客地址 链表 是一种物理存储单元上非连续.非顺序的存储结构,它既可以表示线性结构,也可以用于表示非线性结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的.链表由一系列结点(链表中每 ...

  5. BZOJ 1060: [ZJOI2007]时态同步

    Description 一个有根树,你只能进行增加操作,问你将所有叶节点到根的路径权值相同至少需要增加几次. Sol 我也不知道该叫什么算法... 反正就是记录一下到子节点到当前节点的最大距离统计答案 ...

  6. HTML5实现网页的全屏切换

    使用HTML5提供的JavaScript Api可以实现主流浏览器的全屏和退出全屏操作,封装成进入全屏和退出全屏的函数如下: //进入全屏 function enterFullScreen() { v ...

  7. UVALive 4849 String Phone(2-sat、01染色)

    题目一眼看去以为是4-sat... 题意:给n(n<=3000)个黑方块的坐标,保证黑方块没有公共边.对于每个黑方块选一个角作为结点,使得所选结点满足输入的一个无向图.其中距离为曼哈顿距离.输出 ...

  8. Python 中xrange和range区别

    先看看Python help()的说明 help(range) Help on built-in function range in module __builtin__: range(...) ra ...

  9. 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除。

    题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...

  10. MVC 教程汇总

    http://www.cnblogs.com/powertoolsteam/archive/2015/08/13/4667892.html