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?

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函数计算当前转移需要增加的代价

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 
 7 #define MAXN 100
 8 int n;
 9 char s[MAXN];
10 int dp[MAXN][MAXN][MAXN][2];
11 int pos[3][MAXN],num[3][MAXN];
12 inline int cmd(int i, int j, int k, int p)
13 {
14     return max(0, num[0][p] - i) + max(0, num[1][p] - j) + max(0, num[2][p] - k) - 1;
15 }
16 
17 void init()
18 {
19     for(int i=0;i<MAXN;i++)
20     {
21         for(int j=0;j<MAXN;j++)
22         {
23             for(int z=0;z<MAXN;z++)
24             {
25                 for(int x=0;x<2;x++)
26                 {
27                     dp[i][j][z][x]=10000;
28                 }
29             }
30         }
31     }
32     dp[0][0][0][0]=0;
33 }
34 int main()
35 {
36     init();
37     scanf("%d%s",&n,s+1);
38     for(int i=1;i<=n;i++)
39     {
40         num[0][i]=num[0][i-1];
41         num[1][i]=num[1][i-1];
42         num[2][i]=num[2][i-1];
43         if(s[i]=='V')
44         {
45             pos[0][num[0][i]++]=i;
46         }
47         else if(s[i]=='K')
48         {
49             pos[1][num[1][i]++]=i;
50         }
51         else
52         {
53             pos[2][num[2][i]++]=i;
54         }
55     }
56     int a=num[0][n];
57     int b=num[1][n];
58     int c=num[2][n];
59     for(int i=0;i<=a;i++)
60     {
61         for(int j=0;j<=b;j++)
62         {
63             for(int z=0;z<=c;z++)
64             {
65                 for(int v=0;v<2;v++)
66                 {
67                     if(i<a)
68                     {
69                         dp[i+1][j][z][1]=min(dp[i+1][j][z][1],dp[i][j][z][v]+cmd(i,j,z,pos[0][i]));
70                     }
71                     if(j<b)
72                     {
73                         dp[i][j+1][z][0]=min(dp[i][j+1][z][0],dp[i][j][z][0]+cmd(i,j,z,pos[1][j]));
74                     }
75                     if(z<c)
76                     {
77                         dp[i][j][z+1][0]=min(dp[i][j][z+1][0],dp[i][j][z][v]+cmd(i,j,z,pos[2][z]));
78                     }
79                 }
80             }
81         }
82     }
83     printf("%d
", min(dp[a][b][c][0], dp[a][b][c][1]));
84     return 0;
85 }
原文地址:https://www.cnblogs.com/yinghualuowu/p/6666971.html