题意:给定一个环形字符串,让他把它分成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. li ul 说明

    复制代码代码如下: <div id="menu">  <ul>  <li><a href="#">首页</ ...

  2. 夺命雷公狗-----React---8--react官方提供的组建实现双向绑定

    首先要引入她.. <!DOCTYPE html> <html lang="en"> <head> <meta charset=" ...

  3. 在linux中安装adb和fastboot工具

    我用的是archlinux,在官方的软件仓库里就可以找到对应的包,包的名字叫:android-tools 据说debian系列的软件包是两个,分别是:android-tools-adb, androi ...

  4. LNK1169: one or more multiply defined symbols found

    The build failed due to multiple definitions of one or more symbols. This error is preceded by error ...

  5. HTML5 UI框架Kendo UI Web中如何创建自定义组件(二)

    在前面的文章<HTML5 UI框架Kendo UI Web自定义组件(一)>中,对在Kendo UI Web中如何创建自定义组件作出了一些基础讲解,下面将继续前面的内容. 使用一个数据源 ...

  6. JavaWeb应用开发架构浅谈

    本文就我所经历和使用过的技术和框架, 讨论 Java / Javascript 技术组合构成的Web 应用架构. 一. 概述 Web 应用架构可以划分为两大子系统:前端子系统和后台子系统. 前端子系统 ...

  7. HDU 4280:Island Transport(ISAP模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4280 题意:在最西边的点走到最东边的点最大容量. 思路:ISAP模板题,Dinic过不了. #include & ...

  8. T-SQL 的简单查询语句

    通配符: “_”: 代表匹配一个字符 “%”: 代表匹配多个字符: []:表示范围,可以包含多个数据 [^] 表示取反 “-“  表示范围 逻辑与 and 逻辑或 or  逻辑非 not 聚会函数 : ...

  9. Android 内存泄漏总结

    内存管理的目的就是让我们在开发中怎么有效的避免我们的应用出现内存泄漏的问题.内存泄漏大家都不陌生了,简单粗俗的讲,就是该被释放的对象没有释放,一直被某个或某些实例所持有却不再被使用导致 GC 不能回收 ...

  10. noi 9268 酒鬼

    题目链接:http://noi.openjudge.cn/ch0206/9268/ 题意:有N瓶酒,不能连续喝>=3瓶的酒,问能喝的最大的酒量. f[i][j] 前 I 瓶中连续喝了 j 瓶的最 ...