Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E
Description
Bear Limak prepares problems for a programming competition. Of course, it would be unprofessional to mention the sponsor name in the statement. Limak takes it seriously and he is going to change some words. To make it still possible to read, he will try to modify each word as little as possible.
Limak has a string s that consists of uppercase English letters. In one move he can swap two adjacent letters of the string. For example, he can transform a string "ABBC" into "BABC" or "ABCB" in one move.
Limak wants to obtain a string without a substring "VK" (i.e. there should be no letter 'V' immediately followed by letter 'K'). It can be easily proved that it's possible for any initial string s.
What is the minimum possible number of moves Limak can do?
The first line of the input contains an integer n (1 ≤ n ≤ 75) — the length of the string.
The second line contains a string s, consisting of uppercase English letters. The length of the string is equal to n.
Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK".
4
VKVK
3
5
BVVKV
2
7
VVKEVKK
3
20
VKVKVVVKVOVKVQKKKVVK
8
5
LIMAK
0
In the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is:
- Swap two last letters. The string becomes "VKKV".
- Swap first two letters. The string becomes "KVKV".
- Swap the second and the third letter. The string becomes "KKVV". Indeed, this string doesn't have a substring "VK".
In the second sample, there are two optimal sequences of moves. One is "BVVKV" → "VBVKV" → "VVBKV". The other is "BVVKV" → "BVKVV" → "BKVVV".
In the fifth sample, no swaps are necessary.
题意:给一个字符串,我们可以移动其中的字符,使得里面字符没有VK的最少次数是多少
解法:如果我们移动K,那么就没必要再选V移动了
dp[i][j][z][v]表示当前有i个V,j个K,z个其他字符,0表示当前字符不是V,1表示当前是V时,转移的最小代价
我们处理一下V,K的位置关系,然后转移就行,具体代码可以看明白
cmd函数计算当前转移需要增加的代价
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; #define MAXN 100
int n;
char s[MAXN];
int dp[MAXN][MAXN][MAXN][];
int pos[][MAXN],num[][MAXN];
inline int cmd(int i, int j, int k, int p)
{
return max(, num[][p] - i) + max(, num[][p] - j) + max(, num[][p] - k) - ;
} void init()
{
for(int i=;i<MAXN;i++)
{
for(int j=;j<MAXN;j++)
{
for(int z=;z<MAXN;z++)
{
for(int x=;x<;x++)
{
dp[i][j][z][x]=;
}
}
}
}
dp[][][][]=;
}
int main()
{
init();
scanf("%d%s",&n,s+);
for(int i=;i<=n;i++)
{
num[][i]=num[][i-];
num[][i]=num[][i-];
num[][i]=num[][i-];
if(s[i]=='V')
{
pos[][num[][i]++]=i;
}
else if(s[i]=='K')
{
pos[][num[][i]++]=i;
}
else
{
pos[][num[][i]++]=i;
}
}
int a=num[][n];
int b=num[][n];
int c=num[][n];
for(int i=;i<=a;i++)
{
for(int j=;j<=b;j++)
{
for(int z=;z<=c;z++)
{
for(int v=;v<;v++)
{
if(i<a)
{
dp[i+][j][z][]=min(dp[i+][j][z][],dp[i][j][z][v]+cmd(i,j,z,pos[][i]));
}
if(j<b)
{
dp[i][j+][z][]=min(dp[i][j+][z][],dp[i][j][z][]+cmd(i,j,z,pos[][j]));
}
if(z<c)
{
dp[i][j][z+][]=min(dp[i][j][z+][],dp[i][j][z][v]+cmd(i,j,z,pos[][z]));
}
}
}
}
}
printf("%d\n", min(dp[a][b][c][], dp[a][b][c][]));
return ;
}
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) E的更多相关文章
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 菜鸡只会ABC!
Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) 全场题解 菜鸡只会A+B+C,呈上题解: A. Bear and ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C. Bear and Different Names 贪心
C. Bear and Different Names 题目连接: http://codeforces.com/contest/791/problem/C Description In the arm ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) B - Bear and Friendship Condition 水题
B. Bear and Friendship Condition 题目连接: http://codeforces.com/contest/791/problem/B Description Bear ...
- 【树形dp】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) B. Bear and Tree Jumps
我们要统计的答案是sigma([L/K]),L为路径的长度,中括号表示上取整. [L/K]化简一下就是(L+f(L,K))/K,f(L,K)表示长度为L的路径要想达到K的整数倍,还要加上多少. 于是, ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)
A 模拟 B 发现对于每个连通块,只有为完全图才成立,然后就dfs C 构造 想了20分钟才会,一开始想偏了,以为要利用相邻NO YES的关系再枚举,其实不难.. 考虑对于顺序枚举每一个NO/YES, ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)A B C 水 并查集 思路
A. Bear and Big Brother time limit per test 1 second memory limit per test 256 megabytes input stand ...
- 【构造】Codeforces Round #405 (rated, Div. 1, based on VK Cup 2017 Round 1) A. Bear and Different Names
如果某个位置i是Y,直接直到i+m-1为止填上新的数字. 如果是N,直接把a[i+m-1]填和a[i]相同即可,这样不影响其他段的答案. 当然如果前面没有过Y的话,都填上0就行了. #include& ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) D
Description A tree is an undirected connected graph without cycles. The distance between two vertice ...
- Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1) C
Description In the army, it isn't easy to form a group of soldiers that will be effective on the bat ...
随机推荐
- Robot Framework自己主动化測试框架之我见
一些自己主动化測试现状: 盲目的去做自己主动化,终于以失败告终. 觉得是能提高效率的事情.却推广不下去: 事实上上述问题产生的原因是: 自己主动化測试案例稳定性不高,可维护性比較差: 自己主动化測试工 ...
- delphi 中OutputDebugString 函数的妙用(使用DebugView或者Pascal Analyzer软件,在运行过程中就能监视和捕捉日志,而且通过网络就能监视)
原文地址 https://www.peganza.com/delphi-and-outputdebugstring.html 曾经想要实时监控您的Delphi应用程序,并能够查看日志消息吗?当然,您始 ...
- java上传文件,提交表单必须要设置enctype="multipart/form-data"
表单中enctype="multipart/form-data"的意思,是设置表单的MIME编码.默认情况,这个编码格式是application/x-www-form-urlenc ...
- vue 使用html2canvas将DOM转化为图片
一.前言 我发现将DOM转化为图片是一个非常常见的需求,而自己手动转是非常麻烦的,于是找到了html2canvas这个插件,既是用得比较多的也是维护得比较好的一个插件. 注意:版本比较多,这里介绍最新 ...
- Javascript中两种最通用的定义类的方法
在Javascript中,一切都是对象,包括函数.在Javascript中并没有真正的类,不能像C#,PHP等语言中用 class xxx来定义.但Javascript中提供了一种折中的方案:把对象定 ...
- iOS NSString拼接字符串
NSString* str_C; // 结果字符串NSString* str_A, str_B; //已存在的字符串,需要将str_A和str_B连接起来 //方法1 str_C = [NSStrin ...
- I.MX6 boot from Micro SD
/***************************************************************************** * I.MX6 boot from Mic ...
- DPI和PPI
写在前面 各种手机测频机构或者相关资讯老是谈及一个概念:ppi和dpi,通常总会忽略,只是稍微明白,这参数越高,说明屏幕分辨率越高:很长时间都止步如此:但作为一个iOS开发者,岂能止步如此,万一别人问 ...
- vue-router 基本知识点
vue-router就是将组件映射到路由,然后告诉vue-router在哪里渲染它们. 默认路由出口 <router-view></router-view> 使用router- ...
- Oracle的db.properties文件
转自:https://blog.csdn.net/lssqk/article/details/79133829