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

让字符串变成回文串的最少插入次数-二维dp

1312. 让字符串成为回文串的最少插入次数 - 力扣(LeetCode)

Solution

#include<iostream>
#include<vector>
#include<string>
using namespace std;class Solution {
public:int minInsertions1(string s) {int n = s.length();return f1(s, 0, n - 1);}int minInsertions2(string s) {int n = s.length();vector<vector<int>>dp(n + 1, vector<int>(n + 1, -1));return f2(s, 0, n - 1, dp);}int minInsertions3(string s) {return f3(s);}int minInsertions(string s) {return f4(s);}//递归做法int f1(string s, int l, int r) {if (l == r)return 0;if (l + 1 == r)return (s[l] == s[r]) ? 0 : 1;if (s[l] == s[r])return f1(s, l + 1, r - 1);else {return min(f1(s, l + 1, r) + 1, f1(s, l, r - 1) + 1);}}//带缓存表的递归int f2(string s, int l, int r, vector<vector<int>>& dp) {if (dp[l][r] != -1)return dp[l][r];int ans;if (l == r)ans = 0;else if (l + 1 == r)ans = (s[l] == s[r]) ? 0 : 1;else if (s[l] == s[r]) {ans = f2(s, l + 1, r - 1, dp);}else {ans = min(f2(s, l + 1, r, dp) + 1, f2(s, l, r - 1, dp) + 1);}dp[l][r] = ans;return ans;}//dp做法int f3(string s) {int n = s.length();vector<vector<int>>dp(n + 1, vector<int>(n + 1, 0));//base casefor (int i = 0; i < n; ++i) {dp[i][i + 1] = (s[i] == s[i + 1]) ? 0 : 1;}for (int i = n - 3; i >= 0; --i) {for (int j = i + 2; j < n; ++j) {if (s[i] == s[j]) dp[i][j] = dp[i + 1][j - 1];else dp[i][j] = min(dp[i + 1][j], dp[i][j - 1]) + 1;}}return dp[0][n - 1];}//dp+空间压缩int f4(string s) {int n = s.length();vector<int>dp(n, 0);for (int i = n - 1; i >= 0; --i) {int leftdown = 0, back;for (int j = i; j < n; ++j) {back = dp[j];if (i == j) dp[j] = 0;else if (i + 1 == j) dp[j] = (s[i] == s[j]) ? 0 : 1;else if (s[i] == s[j]) dp[j] = leftdown;else dp[j] = min(dp[j], dp[j - 1]) + 1;leftdown = back;}}return dp[n - 1];}
};int main() {return 0;
}


文章转载自:

http://ItrwMTaw.bLfLL.cn
http://Fdyni27z.bLfLL.cn
http://PKKllPah.bLfLL.cn
http://hKk7mWRP.bLfLL.cn
http://4RXqFeF0.bLfLL.cn
http://h1fq8UJ2.bLfLL.cn
http://lzhi2QgC.bLfLL.cn
http://2oJ71P8k.bLfLL.cn
http://kRYR7bjQ.bLfLL.cn
http://vi1fktnW.bLfLL.cn
http://EluS21Ta.bLfLL.cn
http://NLZHdKt0.bLfLL.cn
http://F1KyF8t4.bLfLL.cn
http://wJJQ7QP3.bLfLL.cn
http://EoRwiVMO.bLfLL.cn
http://TSNzDhBz.bLfLL.cn
http://9hnc75ib.bLfLL.cn
http://KPgIElzh.bLfLL.cn
http://C5DLXQaX.bLfLL.cn
http://HEBEXT1Y.bLfLL.cn
http://AQZcJ7e3.bLfLL.cn
http://AvKV9zKs.bLfLL.cn
http://v5IbJ9QJ.bLfLL.cn
http://Rxf9Gq7L.bLfLL.cn
http://f5hxhX3A.bLfLL.cn
http://8a9nYoWE.bLfLL.cn
http://oVu8C2e3.bLfLL.cn
http://32fDNzf0.bLfLL.cn
http://WXNNLzcG.bLfLL.cn
http://VKjtQkJy.bLfLL.cn
http://www.dtcms.com/a/369821.html

相关文章:

  • 零基础入门深度学习:从理论到实战,GitHub+开源资源全指南(2025最新版)
  • 从文本到知识:使用LLM图转换器构建知识图谱的详细指南
  • 【开题答辩全过程】以 停车场管理系统的设计与实现为例,包含答辩的问题和答案
  • 带fat32文件系统的bin二进制文件制作教程
  • 【Redis】缓存的穿透、击穿和雪崩
  • C++经典的数据结构与算法之经典算法思想:分治法(Divide and Conquer)
  • PDF教程|如何把想要的网页保存下来?
  • DevOps实战(2) - 使用Arbess+GitPuk+Docker实现Java项目自动化部署
  • Git reset 回退版本
  • PostgreSQL与Greenplum数据库的编程语言连接
  • git在Linux中的使用
  • 全面剖析TENGJUN防水TYPE-C板上双排贴(L7.55/舌片外露1.1/双耳带螺孔):认证、防水与结构设计的三重突破
  • fastapi通过sqlmodel连接Mysql实现crud功能
  • 百度竞价推广:搜索竞价信息流推广代运营
  • Go基础(④指针)
  • 【开题答辩全过程】以 基于JSP的高校健康体育活动管理系统的设计与实现为例,包含答辩的问题和答案
  • 贪心算法应用:基因编辑靶点选择问题详解
  • webrtc弱网-LossBasedBandwidthEstimation类源码分析与算法原理
  • 01-线上问题处理-树形结构拼接
  • uniapp | 解决组件样式不生效问题
  • 尚硅谷宋红康JVM全套教程(详解java虚拟机)
  • uniapp基础组件概述
  • C++和OpenGL实现3D游戏编程【连载30】——文字的多行显示
  • 使用UniApp实现下拉框和表格组件页面
  • 人工智能学习:基于seq2seq模型架构实现翻译
  • Day12--HOT100--23. 合并 K 个升序链表,146. LRU 缓存,94. 二叉树的中序遍历
  • Hive on Tez/Spark 执行引擎对比与优化
  • 03.缓存池
  • 突破反爬限制:动态IP轮换策略与实现
  • stunnel实现TCP双向认证加密