博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
CodeForces-771D-Bear and Company
阅读量:6292 次
发布时间:2019-06-22

本文共 2707 字,大约阅读时间需要 9 分钟。

题目地址 :

题意:一个字符串,交换最少的次数,使得不存在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).

地址:

1 #include 
2 #include
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
View Code

 

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

你可能感兴趣的文章
Linux终端中设置vi编辑命令
查看>>
setup vaio winxp
查看>>
EBS FORM(10g)开发步骤
查看>>
Java 旋转数组查找旋转点和任意元素(元素可重复)
查看>>
悲观锁和乐观锁详解
查看>>
KV数据存储:持久化
查看>>
Data Structures with C++ Using STL Chapter 3算法概述---笔记
查看>>
VS2010 ,工程文件减肥
查看>>
国外程序员收集整理的PHP资源大全
查看>>
C#对图片文件的压缩、裁剪操作初探
查看>>
linux磁盘满时,如何定位并删除文件
查看>>
IOS 面试 --- 动画 block
查看>>
二叉树路径和
查看>>
图像处理之霍夫变换(直线检測算法)
查看>>
【AllJoyn专题】基于AllJoyn和Yeelink的传感器数据上传与指令下行的研究
查看>>
收集:搜罗或看到的搞笑桥段
查看>>
Windows 安装 psutil
查看>>
IIS环境下如何批量添加、修改、删除绑定的域名
查看>>
C++继承与派生(原理归纳)
查看>>
struts2.1.6教程七、国际化
查看>>