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 ...
随机推荐
- Hibernate 简单使用
首先在数据库中创建相应的表,脚本如下: create table Student (id int primary key, sName ), sNO ), sex ), email )) 在Myecl ...
- s3c2440 移值u-boot-2016.03 第4篇 支持NAND flash 识别
1, /include/configs/smdk2440.h 中添加 #define CONFIG_CMD_NAND 编译 drivers/mtd/nand/built-in.o: In functi ...
- SVN merge的主干,分支的相互合并操作
本文只研究了 在本地如何进行主干,分支的相互合并 的操作:从主干到分支,从分支到主干. 本地客户端工具是tortoisesvn 测试用例. 1.本地添加test文件夹 在test文件夹下分别建立tru ...
- 2x2矩阵相乘模版
由于Unity只有4x4矩阵,今天要做一个2x2矩阵的旋转,居然忘了顺序.故写下作为模版记录. 顺序: 下面是使用其进行旋转的C#代码: public struct Position { public ...
- filebeat安装与基础用法
来自官网,版本为1.2 下载rpm包并安装 wget -c https://download.elastic.co/beats/filebeat/filebeat-1.2.3-x86_64.rpm r ...
- python 学习笔记八 进程和线程 (进阶篇)
什么是线程(thread)? 线程是操作系统能够进行运算调度的最小单位.它被包含在进程之中,是进程中的实际运作单位.一条线程指的是进程中一个单一顺序的控制流,一个进程中可以并发多个线程,每条线程并行执 ...
- RCP:如何移除Search对话框中不需要的项
前言 很久没写文章了,准备写一系列关于Eclipse RCP /Plugin的文章. 这些文章都是trouble shooting性质的,不准备写的很细,当你碰到这样的问题,google到时,能帮你把 ...
- 配置eclipse使能打开当前文件所在目录
配置方法如下:Run -> External Tools -> External Tools Configurations...右键Program,new 在右边的界面中: Locatio ...
- zabbix通过API创建交换机模板,ifAdminStatus;ifOperStatus;ifInUcastPkts;ifAlias
最终效果: 目的: 通过zabbix的Latest data查看主机就可以看到其监控结果. 监控项: # 管理状态 IF-MIB::ifAdminSt ...
- php中session机制的详解
[补充]session_start()要放在php最前面,header()函数也要放在session_start()之后. [读了下面的文章转载的文章后自己的理解]: 1,通过phpinfo()函数可 ...