HDU 4898 The Revenge of the Princess’ Knight ( 2014 Multi-University Training Contest 4 )
题意:给定一个环形字符串,让他把它分成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 )的更多相关文章
- 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 ...
- hdu 4898 The Revenge of the Princess’ Knight
传送阵:http://acm.hdu.edu.cn/showproblem.php?pid=4898 题目大意:一个首尾相连的字符串,将其分为k个子串,使得最大的字串最小 将所有子串排序,输出第k小即 ...
- 【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 ...
- 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 ...
- HDU 3987 Harry Potter and the Forbidden Forest(边权放大法+最小割)
Harry Potter and the Forbidden Forest Time Limit: 5000/3000 MS (Java/Others) Memory Limit: 65536/ ...
- 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 ...
- 【HDU - 1029】Ignatius and the Princess IV (水题)
Ignatius and the Princess IV 先搬中文 Descriptions: 给你n个数字,你需要找出出现至少(n+1)/2次的数字 现在需要你找出这个数字是多少? Input ...
- 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 ...
- hdu1027 Ignatius and the Princess II (全排列 & STL中的神器)
转载请注明出处:http://blog.csdn.net/u012860063 题目链接:http://acm.hdu.edu.cn/showproblem.php? pid=1027 Ignatiu ...
随机推荐
- Git使用方法
一:Git是什么? Git是目前世界上最先进的分布式版本控制系统. 二:SVN与Git的最主要的区别? SVN是集中式版本控制系统,版本库是集中放在中央服务器的,而干活的时候,用的都是自己的电脑,所以 ...
- C#学校班级自动升级实现代码
代码逻辑如下: //班级自动升级 //获取该学校还没有毕业的班级 List<ClassInfoes> classinfoeslist = classinfoesbll.GetList(Sc ...
- visual studio code + Nodejs + Typescritpt + angularjs2 + bootstrap 环境搭建/Elementary os
1.apt-get update 99% waiting for headers卡住了,fix如下 sudo apt-get clean cd /var/lib/apt sudo mv lists l ...
- 批处理命令——if
[1]if命令简介 if,大家肯定见多了,一个没有用过if的程序员不是一个好产品经理(呵呵~~). if,判断选择的作用,自从写了几年代码,我才真正弄明白了一件事,为什么当初上学时候考试总要做那么多的 ...
- WLST 命令和变量
下列部分将详细描述 WLST 命令和变量.主题包括: WSLT 命令类别概述 浏览命令 控制命令 部署命令 诊断命令 编辑命令 信息命令 生命周期命令 节点管理器命令 树命令 W ...
- java ftp下载文件
1.使用官方正规的jar commons-net-1.4.1.jar jakarta-oro-2.0.8.jar 注意:使用ftp从windows服务器下载文件和从linux服务器下载文件不一样 2. ...
- eclipse编辑struts.xml 代码提示
先确定xml文件 window-preferences-查询catalog 点击add 关于这个Location 先找到你下载的struts压缩包 然后找到 解压这个jar包 你会得到一些dtd文件 ...
- LeetCode----172. Factorial Trailing Zeroes(Java)
package singlenumber136; //Given an array of integers, every element appears twice except for one. F ...
- mac攻略(五) -- 使用brew配置php7开发环境(mac+php+apache+mysql+redis)
前面介绍过基本的配置,后来我又从网上查找了很多资料,经过不断的摸索,下面做了一个总结,希望能对大家提供些许帮助(Mac版本是sierra) 一.mac系统会自带git,而我们要做的是自己安装git ...
- windows消息和消息队列
windows消息和消息队列 转自:http://blog.163.com/zhangjie_0303/blog/static/990827062010113062446767/ 与基于MS - DO ...