动态规划-Minimum Insertion Steps to Make a String Palindrome
2024-08-26 22:48:31
2020-01-05 11:52:40
问题描述:
问题求解:
好像多次碰到类似的lcs的变种题了,都是套上了回文的壳。这里再次记录一下。
其实本质就是裸的lcs,就出结果了。
public int minInsertions(String s) {
StringBuffer sb = new StringBuffer(s);
String b = sb.reverse().toString();
return s.length() - lcs(s, b);
} public int lcs(String str1, String str2) {
int len1 = str1.length();
int len2 = str2.length();
int c[][] = new int[len1+1][len2+1];
for (int i = 0; i <= len1; i++) {
for( int j = 0; j <= len2; j++) {
if(i == 0 || j == 0) {
c[i][j] = 0;
} else if (str1.charAt(i-1) == str2.charAt(j-1)) {
c[i][j] = c[i-1][j-1] + 1;
} else {
c[i][j] = Math.max(c[i - 1][j], c[i][j - 1]);
}
}
}
return c[len1][len2];
}
最新文章
- 重新编译jdk源码,启用debug信息
- atitit.编程语言 程序语言 的 工具性 和 材料性 双重性 and 语言无关性 本质
- Linux下shell颜色配置
- 洛谷P3386 【模板】二分图匹配
- mysql Unknown table engine &#39;InnoDB&#39;解决办法
- 有意思的C宏
- python类继承
- bootstrap-wysihtml5设置值
- 云计算学习(5-1)云平台产品介绍-华为的FusionCloud产品
- Python 词典增加和删除
- 代码覆盖率测试及 GitHub 自动化集成
- a simple game based on RT-Thread
- 【Python】【爬虫】如何学习Python爬虫?
- python学习之【16】网络编程
- Gradle初步
- 一个try可以跟进多个catch语句,用于处理不同情况,当一个try只能匹配一个catch
- Chandy-Lamport_algorithm
- web api 支持cors
- python入门22 pymssql模块(python连接sql server查询)
- Oracle 同环比排除分母0