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函数计算当前转移需要增加的代价
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\n", min(dp[a][b][c][0], dp[a][b][c][1])); 84 return 0; 85 }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
· 三行代码完成国际化适配,妙~啊~