CodeForces-771D-Bear and Company

题目地址 :http://codeforces.com/problemset/problem/771/D

题意:一个字符串,交换最少的次数,使得不存在VK

题解:DP

官方题解很牛B。

Letters different than 'V' and 'K' are indistinguishable, so we can treat all of them as the same letter 'X'.

We will try to build the final string from left to right Let dp[v][k][x] denote the number of moves needed to move first v letters 'V', first kletters 'K' and first x letters 'X' to the beginning of the string (those letters should become first v + k + x letters of the string). We should also remember the last used letter (to ensure that there is no 'K' just after 'V') so let's extend the state to dp[v][k][x][lastLetter] (or it can be dp[v][k][x][is_the_last_letter_V]).

To move from a state, we should consider taking the next 'K' (i.e. the k + 1-th letter 'K' in the initial string), the next 'V' or the next 'X'. Of course, we can't take 'K' if the last used letter was 'V'.

The last step is to see how we should add to the score when we add a new letter. It turns out that it isn't enough to just add the difference between indices (where the letter was and where it will be) and the third sample test ("VVKEVKK") showed that. Instead, we should notice that we know which letters are already moved to the beginning (first k letters 'K' and so on) so we know how exactly the string looks like currently.

For example, let's consider the string "VVKXXVKVV" and moving from the state v = 4, k = 1, x = 1 by taking a new letter 'K'. We know that first 4 letters 'V', 1 letter 'K' and 1 letter 'X' are already moved to the beginning. To move the next letter 'K' (underlined in blue on the drawing below) to the left, we must swap it with all not-used letters that were initially on the left from this 'K'. Counting them in linear time gives the total complexity O(n4) but you can also think a bit and get O(n3) - it's quite easy but it wasn't required to get AC. On the drawing below, used letters are crossed out. There is only 1 not-crossed-out letter on the left from 'K' so we should increase the score by 1 (because we need 1 swap to move this 'K' to the x + k + v + 1-th position).

地址:http://codeforces.com/blog/entry/51068

 1 #include <iostream>
 2 #include <cstring>
 3 const int inf = 0x3f3f3f3f;
 4 using namespace std;
 5 int pk[80];
 6 int pv[80],px[80];
 7 int dp[80][80][80][2];
 8 int vn = 0,kn = 0,xn = 0;
 9 int vs[80],ks[80],xs[80];
10 int N;
11 string s;
12 int get(int num,int flag,int v,int k,int x)
13 {
14     int ans = 0,tans[3];
15     tans[0] = max(vs[num]-v,0);
16     tans[1] = max(ks[num]-k,0);
17     tans[2] = max(xs[num]-x,0);
18     for (int i = 0;i<=2;i++)
19         if (i!=flag) ans+=tans[i];
20     return ans;
21 }
22 int main()
23 {
24     cin >> N;
25     cin >> s;
26     for (int i = 0 ;i<N; i++)
27     {
28         if (i!=0) {vs[i] = vs[i-1];ks[i] = ks[i-1];xs[i] = xs[i-1];}
29         if (s[i] == 'K') {pk[++kn] = i;ks[i]++;}
30         else if (s[i] == 'V') {pv[++vn] = i;vs[i]++;}
31         else {px[++xn] = i;xs[i]++;}
32     }
33     memset(dp,inf,sizeof(dp));
34     dp[0][0][0][0] = 0;
35     for (int v = 0 ; v<=vn; v++)
36         for (int k = 0; k<=kn; k++)
37             for (int x = 0; x <= xn; x++)
38     {
39         if (v < vn) dp[v+1][k][x][1] = min(min(dp[v][k][x][0],dp[v][k][x][1]) + get(pv[v+1],0,v,k,x),dp[v+1][k][x][1]);
40         if (k < kn) dp[v][k+1][x][0] = min( dp[v][k][x][0] + get(pk[k+1],1,v,k,x),dp[v][k+1][x][0]);
41         if (x < xn) dp[v][k][x+1][0] = min(min(dp[v][k][x][0],dp[v][k][x][1]) + get(px[x+1],2,v,k,x),dp[v][k][x+1][0]);
42     }
43     cout << min(dp[vn][kn][xn][0],dp[vn][kn][xn][1]) <<endl;
44 }
View Code

 

转载于:https://www.cnblogs.com/HITLJR/p/6598607.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值