题意:给定一个环形字符串,让他把它分成k份,使得最大的字典序 最小。

思路:二分答案,首先很明显答案所有可能是 n*n种  排序可以先求出最长公共前缀,这样比较就只需要比较公共前缀的下一位就能比较出两种答案的字典序大小,这里公共前缀我用 (2*n)*(2*n) DP 求出 。(也可用后缀数组)。接下来就是判断了:

这里二分出来的答案就是字典序的上界,(对于没一个位置作为起点,长度越长字典序越大)那么对于当前答案每个位置都可以求出一个能向后跳的最大值(根据如果以这个点做一个串的起点那么字典序要小于我们二分到的答案)。

那么问题就转换成能不能找到一个起点恰好跳k次回到本身。

首先我们假设cur[i] 为i能向右跳最大距离。如果所有cur[i]都大于0,那么我们只要找到最小需要跳的次数 x   如果x<=k 那么我们肯定就能通过调整步长使得恰好k次能到达(如果总点数小于k  当然无解)最小的k,可以通过枚举每个起点然后尽量往后跳看要几次即可。

为什么能够尽量往后跳来当最少需要的次数呢:因为如果该字符串中如果存在一个字符小于二分到的答案首字母(那就说明以这个字母开始的所有字符串字典序均小于二分到的答案),那么我们就能通过一次跳跃就完成(这里因为有枚举起点肯定会枚举到)。  如果所有字母均等于答案的首字母(这里不可能存在大于答案首字母的字符,如果存在这种字符,他必定不能往后跳,就删除了),那么每次往后跳就为最优。

那么当cur[i]==0时 表示他不能跳到别人别人也不能跳到他那么就删除这个点如果一个点跳跃区间有覆盖这个点  这跳跃区间减去1 。最多n次删除效率为(n*n)

#include <iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<vector>
#define N 2010
using namespace std;
char str[N];
struct node {
int l, r;
int size;
char charat(int x) {
if (x < size)
return str[l + x];
else
return ;
}
} s[N * N];
int lcp[N][N];
int n, k;
int min(int x, int y) {
return x < y ? x : y;
}
int max(int x, int y) {
return x > y ? x : y;
}
int LCP(node a, node b) {
int tmp = lcp[a.l][b.l];
tmp = min(tmp, a.size);
tmp = min(tmp, b.size);
return tmp;
}
bool cmp(node a, node b) {
int tmp = LCP(a, b);
return a.charat(tmp) < b.charat(tmp);
}
void init() {
scanf("%d%d", &n, &k);
scanf(" %s", str);
for (int i = n; i < n * ; ++i)
str[i] = str[i - n];
for (int i = ; i <= n + n; ++i)
lcp[i][n + n] = lcp[n + n][i] = ;
for (int i = n + n - ; i >= ; --i)
for (int j = n + n - ; j >= ; --j) {
if (str[i] == str[j])
lcp[i][j] = lcp[i + ][j + ] + ;
else
lcp[i][j] = ;
} }
bool check(int mid) {
vector<int> cur;
for (int i = ; i < n; ++i) {
int tmp = lcp[s[mid].l][i];
if (tmp >= s[mid].size) {
cur.push_back(s[mid].size);
continue;
}
if (s[mid].charat(tmp) > str[i + tmp])
{
cur.push_back(n);
}
else
cur.push_back(tmp);
}
while (true) {
int flag = ;
for (int i = ; i < cur.size(); ++i) {
if (cur[i] == ) {
flag = ;
for (int j = ; j < i; ++j)
if (j + cur[j] >= i)
cur[j]--;
for (int j = i + ; j < cur.size(); ++j)
if (j + cur[j] >= i + cur.size())
cur[j]--; cur.erase(i + cur.begin());
break;
}
}
if (flag)
break;
}
if (cur.size() < k)
return false;
int len = cur.size();
for (int i = ; i < cur.size(); ++i) { int cnt = ;
for (int j = i; j < i + len; j += cur[j % len])
cnt++; if (cnt <= k)
return true;
} return false;
}
void solve() {
int tail = ;
for (int i = ; i < n; ++i) {
for (int j = i; j < i + n; ++j) {
s[tail].size = j - i + ;
s[tail].l = i;
s[tail++].r = j;
}
}
sort(s, s + tail, cmp);
int l = , r = tail - ;
while (r - l > ) {
int mid = (l + r) >> ;
if (check(mid))
r = mid;
else
l = mid;
}
int ans;
if (check(l))
ans = l;
else
ans = r;
for (int i = ; i < s[ans].size; ++i)
printf("%c", s[ans].charat(i));
puts("");
}
int main() {
int tt;
scanf("%d", &tt);
while (tt--) {
init();
solve();
}
return ;
}

HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )的更多相关文章

  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

    传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4898 题目大意:一个首尾相连的字符串,将其分为k个子串,使得最大的字串最小 将所有子串排序,输出第k小即 ...

  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 4897 Little Devil I(树链剖分)(2014 Multi-University Training Contest 4)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4897 Problem Description There is an old country and ...

  5. HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)

    Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65536/ ...

  6. HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)

    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. HDU 4900 NO ACM NO LIFE(概率+枚举+搜索)(2014 Multi-University Training Contest 4)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4900 Problem Description There is an old country and ...

  9. hdu1027 Ignatius and the Princess II (全排列 &amp; STL中的神器)

    转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...

随机推荐

  1. 使用 gulp 搭建前端环境入门篇(转载)

    本文转载自: 使用 gulp 搭建前端环境入门篇

  2. 更新数据前jquery如何填充数据到表单域中

    $("#p_city option[value='${project.city}']").attr("selected","selected" ...

  3. socket.io稳定性及事件测试

    socket.io测试报告 1.socekt.io能坚持多久 将服务器上的socekt.io代码从早上9:30分开始运行到晚上18点,每100毫秒发送一条数据,数据大概15个字符,同时开启5个连接 结 ...

  4. jsp通过jQuery返回json数据到页面

    1.首先要导入json的包,自己去网站找,总共6个jar包! 2. 在servlet里的写法(仅共参考) JSONObject json = new JSONObject(); String a = ...

  5. Retrieve失败解决办法一例

    错误:The service '/XRMServices/2011/OrganizationData.svc' cannot be activated due to an exception duri ...

  6. Windows Azure Table storage 之 动态Table类 DynamicTableEntity

    在一般情况下,当我们在.net中使用Azure table storage的时候都会为该表建立一个TableEntity的派生类,如下所示. public class CustomerEntity : ...

  7. Objective-C语言内存管理

    • Objective-C为每个对象提供一个内部计数器,这个计数器跟踪对象的引用次数.所有类都继承自 NSObject 的对象retain和release方法. 如果使用了new.alloc或copy ...

  8. 设置Ubuntu 14.04右键终端的方法

    设置Ubuntu 14.04右键终端的方法如下: 首先要安装一个包,即可在右键里面添加一个"打开终端"的菜单. sudo apt-get install nautilus-open ...

  9. s3c2440 移值新内核 linux-4.1.36

    arm-linuxgcc version 4.3.2 经过试验,最高可以编译到 linux-4.1.36 ,在高的版本会有错误 ,可能是 GCC 编译器版本较低造成. 解压比较麻烦还要装一个 xz x ...

  10. Sprint第二个冲刺(第八天)

    一.Sprint介绍 任务进度: 二.Sprint周期 看板: 燃尽图: