动态规划——Freedom Trail
题目:https://leetcode.com/problems/freedom-trail/
额。。。不解释大意了,题目我也不想写过程了有点繁琐,直接给出代码:
public int findRotateSteps(String ring, String key) {
int rlen = ring.length();
int klen = key.length();
int[][]dp = new int[klen+1][rlen+1];
int temp = 0,step = 0,diff = 0;
for(int i = 0;i<=klen;i++) {
for(int j = 1;j<=rlen;j++)
if(i==0)dp[i][j] = 0;
else dp[i][j] = Integer.MAX_VALUE;
}
for(int i = 1;i<=klen;i++) {
for(int j = 1;j<=rlen;j++) {
temp = Integer.MAX_VALUE;
if(ring.charAt(j-1)==key.charAt(i-1)) {
if(i==1)temp = Math.min(j-1,rlen+1-j);
else {
for(int k = 1;k<=rlen;k++)
if(dp[i-1][k]!=Integer.MAX_VALUE) {
diff = Math.abs(j-k);
step = Math.min(diff,rlen-diff);
temp = Math.min(temp,step+dp[i-1][k]);
}
}
}
dp[i][j] = temp;
}
}
/*
for(int i = 1;i<=klen;i++) {
for(int j = 1;j<=rlen;j++)
System.out.print(dp[i][j]+" ");
System.out.println();
}*/
int ans = dp[klen][1];
for(int i = 2;i<=rlen;i++) {
ans = ans < dp[klen][i]?ans:dp[klen][i];
}
return ans+klen;
}
动态规划——Freedom Trail的更多相关文章
- [LeetCode] Freedom Trail 自由之路
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- [Swift]LeetCode514. 自由之路 | Freedom Trail
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- 514. Freedom Trail
In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a metal ...
- 514 Freedom Trail 自由之路
详见:https://leetcode.com/problems/freedom-trail/description/ C++: class Solution { public: int findRo ...
- LeetCode 514----Freedom Trail
问题描述 In the video game Fallout 4, the quest "Road to Freedom" requires players to reach a ...
- leetcode动态规划题目总结
Hello everyone, I am a Chinese noob programmer. I have practiced questions on leetcode.com for 2 yea ...
- Leetcode 514.自由之路
自由之路 视频游戏"辐射4"中,任务"通向自由"要求玩家到达名为"Freedom Trail Ring"的金属表盘,并使用表盘拼写特定关键词 ...
- leetcode 学习心得 (2) (301~516)
源代码地址:https://github.com/hopebo/hopelee 语言:C++ 301. Remove Invalid Parentheses Remove the minimum nu ...
- Swift LeetCode 目录 | Catalog
请点击页面左上角 -> Fork me on Github 或直接访问本项目Github地址:LeetCode Solution by Swift 说明:题目中含有$符号则为付费题目. 如 ...
随机推荐
- [转载]如何在ubuntu上使用github
来源:https://blog.csdn.net/tina_ttl/article/details/51326684 https://blog.csdn.net/u013551462/article/ ...
- VS2017添加引用报错
未能正确加载“ReferenceManagerPackage”包. 此问题可能是因配置更改或安装另一个扩展导致的.可通过查看文件“C:\Users\Administrator\AppData\Roam ...
- flask+mod_wsgi+apache在windows上的布署
已经安装过python3.5 1.安装flask: pip install flask 2.安装apache: Apache是开源软件,针对windows环境,它不直接提供编译版本.可以在http:/ ...
- luogu P5303 [GXOI/GZOI2019]逼死强迫症
传送门 只有两行,考虑递推,设\(f_i\)为没有那两个\(1*1\)的,前\(i\)列的方案,可以发现一次可以放一个竖的或两个横的,也就是\(f_i=f_{i-1}+f_{i-2}\) 再设\(g_ ...
- kaldi chain模型的序列鉴别性训练代码分析
chainbin/nnet3-chain-train.cc int main(int argc, char *argv[]) { ... Nnet nnet; ReadKaldiObject(nnet ...
- 通用RSA加密 - PHP+Java+Javascript加密解密
php端生成 公钥私钥 1.openssl genrsa -out rsa_private_key.pem 1024 私钥 2.openssl rsa -in rsa_private_key.p ...
- 项目Alpha冲刺(团队)-第一天冲刺
格式描述 课程名称:软件工程1916|W(福州大学) 作业要求:项目Alpha冲刺(团队)-代码规范.冲刺任务与计划 团队名称:为了交项目干杯 作业目标:描述第一天冲刺的项目进展.问题困难.心得体会 ...
- 算法——八皇后问题(eight queen puzzle)之回溯法求解
八皇后谜题是经典的一个问题,其解法一共有种! 其定义: 首先定义一个8*8的棋盘 我们有八个皇后在手里,目的是把八个都放在棋盘中 位于皇后的水平和垂直方向的棋格不能有其他皇后 位于皇后的斜对角线上的棋 ...
- C# 常用类型校验Validate
using System.Text; using System.Text.RegularExpressions; namespace 落地页测试代码 { public class Validate { ...
- window mysql8.0 zip版本安装
第一步下载安装包 官方下载地址:https://dev.mysql.com/downloads/mysql/ 解压到D盘目录中D://db 第二步配置环境变量 编辑path内容 添加mysql地址 第 ...