题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6739

借鉴了这个网址的题解:https://blog.csdn.net/qq_41785863/article/details/101676640

题意:每个大写字母代表一个技能,每个技能都由三个法球组成(无序的)。现在给出一串技能字符串,要求求出释放所有技能所需的最小按键次数、每输入一个法球按键一次,释放技能时需要按键一次(R)。

思路:因为技能的组成是无序的,也就是每个技能有6种组合方式,可以全部枚举出来。后一个技能也可以全部枚举出来,这样找到相邻技能36种组合中,链接起来时,法球数目最少的。即可以得到我们的状态转移方程。

dp[i][j]表示以j组合形式释放第i种技能时的最少按键次数。

代码如下:

 #include<stdio.h>
#include<iostream>
#include<string.h>
#include<string>
#include<map>
#include<algorithm>
#define mem(a, b) memset(a, b, sizeof(a))
const int MAXN = 1e5 + ;
const int inf = 0x3f3f3f3f;
using namespace std; map<char, string> mp;
char s[MAXN];
int dp[MAXN][];
int dir[][] = {, , ,
, , ,
, , ,
, , ,
, , ,
, , }; int cal(string s, string s1, int x, int y)
{
int ret;
if(s[dir[x][]] == s1[dir[y][]] && s[dir[x][]] == s1[dir[y][]] && s[dir[x][]] == s1[dir[y][]]) ret = ;
else if(s[dir[x][]] == s1[dir[y][]] && s[dir[x][]] == s1[dir[y][]]) ret = ;
else if(s[dir[x][]] == s1[dir[y][]]) ret = ;
else ret = ;
return ret;
} int main()
{
mp['Y'] = "QQQ", mp['V'] = "QQW", mp['G'] = "QQE", mp['C'] = "WWW";
mp['X'] = "QWW", mp['Z'] = "WWE", mp['T'] = "EEE", mp['F'] = "QEE";
mp['D'] = "WEE", mp['B'] = "QWE"; while(scanf("%s", s + ) != EOF)
{
int len = strlen(s + );
mem(dp, inf);
for(int i = ; i < ; i ++)
dp[][i] = ;
for(int i = ; i <= len; i ++)
for(int j = ; j < ; j ++)
for(int k = ; k < ; k ++)
dp[i][j] = min(dp[i][j], dp[i - ][k] + cal(mp[s[i - ]], mp[s[i]], k, j) + );
int ans = inf;
for(int i = ; i < ; i ++)
ans = min(ans, dp[len][i]);
printf("%d\n", ans);
}
return ;
}

HDU6739 Invoker 【dp】的更多相关文章

  1. Kattis - honey【DP】

    Kattis - honey[DP] 题意 有一只蜜蜂,在它的蜂房当中,蜂房是正六边形的,然后它要出去,但是它只能走N步,第N步的时候要回到起点,给出N, 求方案总数 思路 用DP 因为N == 14 ...

  2. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  3. HDOJ 1501 Zipper 【DP】【DFS+剪枝】

    HDOJ 1501 Zipper [DP][DFS+剪枝] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Ja ...

  4. HDOJ 1257 最少拦截系统 【DP】

    HDOJ 1257 最少拦截系统 [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...

  5. HDOJ 1159 Common Subsequence【DP】

    HDOJ 1159 Common Subsequence[DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ...

  6. HDOJ_1087_Super Jumping! Jumping! Jumping! 【DP】

    HDOJ_1087_Super Jumping! Jumping! Jumping! [DP] Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. POJ_2533 Longest Ordered Subsequence【DP】【最长上升子序列】

    POJ_2533 Longest Ordered Subsequence[DP][最长递增子序列] Longest Ordered Subsequence Time Limit: 2000MS Mem ...

  8. HackerRank - common-child【DP】

    HackerRank - common-child[DP] 题意 给出两串长度相等的字符串,找出他们的最长公共子序列e 思路 字符串版的LCS AC代码 #include <iostream&g ...

  9. LeetCode:零钱兑换【322】【DP】

    LeetCode:零钱兑换[322][DP] 题目描述 给定不同面额的硬币 coins 和一个总金额 amount.编写一个函数来计算可以凑成总金额所需的最少的硬币个数.如果没有任何一种硬币组合能组成 ...

随机推荐

  1. Start Failed, Internal error: recovering IDE to the working state after the critical startup error

    Start Failed, Internal error: recovering IDE to the working state after the critical startup error F ...

  2. springboot项目没错,但就是报红叉

    1.报错原因: Description Resource Path Location TypeCannot change version of project facet Dynamic Web Mo ...

  3. 7zip使用相关

    造冰箱的大熊猫@cnblogs 2019/11/2 1.仅存储不压缩 7z a -mx0 compressed.7z FileFolderPath 将FileFolderPath指向的文件或文件夹打包 ...

  4. Python数据类型之数值-Python基础前传(5)

    学习任何一门学科或者手艺,最忌讳的就是想的太多,做的太少: 有很多朋友私信问我:jacky,我们该如何选择Python的课程?或是我们该如何选择Mysql课程?到底谁的课件和书籍才是最好的? 借着今天 ...

  5. ICEM—倾斜孔

    原视频下载:https://yunpan.cn/cS3UGMEscrYpL  访问密码 839b

  6. 大整数乘法(c++)【转载】

    摘自<c++数据结构原理与经典问题求解> #include #include #include using namespace std; //返回位数为size1+size2 int* m ...

  7. oracle中查询某个库中所有的表以及所占的表空间大小

    1. 查某一用户下的表select SEGMENT_NAME,TABLESPACE_NAME,sum(BYTES/1024/1024)||'M' from USER_extents where SEG ...

  8. Sketch教程

    去年夏天开始用Sketch,觉得它放Dock里闪亮亮的很好看,当时笑称是男盆友送我的第一颗小钻石噗哈哈.所以那段时间几乎刷遍了所有关于 Sketch 的网站.文章.教程,之后又在自学设计,因为想和更多 ...

  9. Java并发指南4:Java中的锁 Lock和synchronized

    Java中的锁机制及Lock类 锁的释放-获取建立的happens before 关系 锁是java并发编程中最重要的同步机制.锁除了让临界区互斥执行外,还可以让释放锁的线程向获取同一个锁的线程发送消 ...

  10. LC 983. Minimum Cost For Tickets

    In a country popular for train travel, you have planned some train travelling one year in advance.  ...