当前位置: 首页 > news >正文

C++ | Leetcode C++题解之第514题自由之路

题目:

题解:

class Solution {
public:int findRotateSteps(string ring, string key) {int n = ring.size(), m = key.size();vector<int> pos[26];for (int i = 0; i < n; ++i) {pos[ring[i] - 'a'].push_back(i);}vector<vector<int>> dp(m, vector<int>(n, 0x3f3f3f3f));for (auto& i: pos[key[0] - 'a']) {dp[0][i] = min(i, n - i) + 1;}for (int i = 1; i < m; ++i) {for (auto& j: pos[key[i] - 'a']) {for (auto& k: pos[key[i - 1] - 'a']) {dp[i][j] = min(dp[i][j], dp[i - 1][k] + min(abs(j - k), n - abs(j - k)) + 1);}}}return *min_element(dp[m - 1].begin(), dp[m - 1].end());}
};

http://www.mrgr.cn/news/59366.html

相关文章:

  • Golang | Leetcode Golang题解之第513题找树左下角的值
  • itemStyle.normal.label is deprecated, use label instead.
  • 第二十八节高斯模糊
  • C++ | Leetcode C++题解之第513题找树左下角的值
  • street_gaussians 点云监督
  • crontab定时
  • 群晖系统基本命令
  • Vue全栈开发旅游网项目首页
  • 论1+2+3+4+... = -1/12 的不同算法
  • 通过HBase实现大规模日志数据存储与分析
  • github上传文件代码以及其它github代码
  • 2024年MathorCup妈杯大数据竞赛选题人数发布
  • MATLAB深度学习杂草识别系统
  • Python | Leetcode Python题解之第513题找树左下角的值
  • C++,STL 048(24.10.25)
  • XCode16中c++头文件找不到解决办法
  • SQLI LABS | Less-12 POST-Error Based-Double quotes-String-with twist
  • 第二十六节 直方图均衡化
  • S-Function
  • Python | Leetcode Python题解之第514题自由之路