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. Java 内存区域与内存溢出异常

    一.Java虚拟机内存划分 1.程序计数器 线程私有 可以看做是当前线程所执行的字节码的行号指示器.字节码解释器工作时是通过改变这个计数器的值来选取下一条需要执行的字节码指令. Java虚拟机是通过多 ...

  2. redis的图形界面管理工具

    大部分人都知道redis是一款用在缓存服务器上的软件,它与memcache类似,都可以存储海量的数据,用在大访问量的web网站.聊天记录存放等方面,但是又与memcache不同: 1.缓存数据可以持久 ...

  3. springboot 多数据源(三种数据库连接池--JDBC,dbcp2,Druid)

    本文使用的是springboot2.0(在配置数据源时和springboot1.X略有区别) 首先:springboot默认支持的连接池有dbcp,dbcp2, tomcat, hikari四种连接池 ...

  4. idhttp post 上传或下载时显示进度条(对接idhttp1.OnWork事件)

    通过 idhttp 带进度条上传演示一下,下载和上传原理差不多,说明一下下面例子中的的idhttp 是动态创建的 第一步:添加一个StatusBar或者gauge 进度条,这2个都可以.我用的是 st ...

  5. 对soc-audio体系snd_soc_machine和snd_soc_dai_link简单理解

    ASOC (ALSA system on chip)              // 主要为嵌入式系统专门开发的sound管理体系结构[luther.gliethttp].Digital Audio ...

  6. Android系统设置Android adb 开关的方法【转】

    本文转载自:http://www.wxtlife.com/2015/11/24/Android-set-adb-status/ 想第一时间获取我的最新文章,请关注公众号: 技术特工队 在整机系统开发中 ...

  7. 解决VMware安装Ubuntu的过程中窗口过小无法看到【下一步】按钮的问题

    只要按住ALT键向上拖动窗口

  8. 合并table中某一列相邻的相同的行

    合并table中某一列相邻的相同的行​1. [代码]合并table中某一列相邻的相同的行  <!DOCTYPE html><html>    <head>      ...

  9. poj 1258 Agri-Net 解题报告

    题目链接:http://poj.org/problem?id=1258 题目意思:给出 n 个 farm,每个farm 之间通过一定数量的fiber 相连,问使得所有farm 直接或间接连通的 最少 ...

  10. mediaplayer state

    enum media_player_states { MEDIA_PLAYER_STATE_ERROR = 0, MEDIA_PLAYER_IDLE = 1 << 0, MEDIA_PLA ...