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?

Input

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.

Output

Print one integer, denoting the minimum possible number of moves Limak can do, in order to obtain a string without a substring "VK".

Examples
input
4
VKVK
output
3
input
5
BVVKV
output
2
input
7
VVKEVKK
output
3
input
20
VKVKVVVKVOVKVQKKKVVK
output
8
input
5
LIMAK
output
0
Note

In the first sample, the initial string is "VKVK". The minimum possible number of moves is 3. One optimal sequence of moves is:

  1. Swap two last letters. The string becomes "VKKV".
  2. Swap first two letters. The string becomes "KVKV".
  3. 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的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 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 ...

  4. 【树形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的整数倍,还要加上多少. 于是, ...

  5. Codeforces Round #405 (rated, Div. 2, based on VK Cup 2017 Round 1)

    A 模拟 B 发现对于每个连通块,只有为完全图才成立,然后就dfs C 构造 想了20分钟才会,一开始想偏了,以为要利用相邻NO YES的关系再枚举,其实不难.. 考虑对于顺序枚举每一个NO/YES, ...

  6. 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 ...

  7. 【构造】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& ...

  8. 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 ...

  9. 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 ...

随机推荐

  1. python day-01 (python基础知识1)

    1.计算机组成(参与运算的) 主板+CPU+内存 2.python 简史 解释型语言,弱类型语言(eg:a 可以表示很多意思   a=1  或 a = “小红”) 3.数据类型: int(整数类型): ...

  2. poj 3585 Accumulation Degree(二次扫描和换根法)

    Accumulation Degree 大致题意:有一棵流量树,它的每一条边都有一个正流量,树上所有度数为一的节点都是出口,相应的树上每一个节点都有一个权值,它表示从这个节点向其他出口可以输送的最大总 ...

  3. DeepLearningFlappyBird-深度学习玩游戏-1-环境搭建

    -------------------------------------------------------------------------------------- https://githu ...

  4. RabbitMQ使用简述

    RabbitMQ基于AMQP协议. AMQP:是一个高级抽象层消息通信协议,RabbitMQ是AMQP协议的实现 RabbitMQ使用:Exchange(交换机)根据routing-key(路由选择键 ...

  5. Codeforces Beta Round #25 (Div. 2 Only)E. Test

    E. Test time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...

  6. Delphi通过POST传递参数给PHP

    Delphi代码 ******************************************************************************************* ...

  7. Spring Boot2.0+Redis+Ehcache实现二级缓存

    EHCache 本地缓存 Redis 分布式缓存(可以共享) 一级 Redis 二级Ehcache    当redis挂了 有备胎 反之: 先走本地,本地没有再走网络  尽量少走Redis  效率会高 ...

  8. 织梦DEDE多选项筛选_联动筛选功能的实现_二次开发

    织梦默认的列表页没有筛选功能,但有时候我们做产品列表页的时候,产品的字段比较多,很多人都需要用到筛选功能,这样可以让用户更方便的找到自己所需要的东西,实现这个联动筛选功能需要对织梦进行二次开发,下面就 ...

  9. 以太坊EVM在安全性方面的考虑

    以太坊上用户编写的合约是不可控的,要保证这些合约能够正确执行并且不会影响区块链的稳定,虚拟机需要做安全方面的考虑. 1 在程序执行过程中采取的每个计算步骤都必须提前支付费用, 从而防止DoS攻击.先消 ...

  10. 修改CentOS系统的默认启动级别

    ======修改CentOS系统的默认启动级别====== 现在的Linux系统安装完后就运行在第5个级别,即系统启动后直接进入图形界面,而不用在字符模式下登录后用startx或者xinit来起动图形 ...