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];
}

  

最新文章

  1. 重新编译jdk源码,启用debug信息
  2. atitit.编程语言 程序语言 的 工具性 和 材料性 双重性 and 语言无关性 本质
  3. Linux下shell颜色配置
  4. 洛谷P3386 【模板】二分图匹配
  5. mysql Unknown table engine &#39;InnoDB&#39;解决办法
  6. 有意思的C宏
  7. python类继承
  8. bootstrap-wysihtml5设置值
  9. 云计算学习(5-1)云平台产品介绍-华为的FusionCloud产品
  10. Python 词典增加和删除
  11. 代码覆盖率测试及 GitHub 自动化集成
  12. a simple game based on RT-Thread
  13. 【Python】【爬虫】如何学习Python爬虫?
  14. python学习之【16】网络编程
  15. Gradle初步
  16. 一个try可以跟进多个catch语句,用于处理不同情况,当一个try只能匹配一个catch
  17. Chandy-Lamport_algorithm
  18. web api 支持cors
  19. python入门22 pymssql模块(python连接sql server查询)
  20. Oracle 同环比排除分母0

热门文章

  1. 剑指offer-18-2. 删除链表中重复的结点
  2. 添砖加瓦:简述ELK部署
  3. 解决appium升级后不支持使用name定位的问题
  4. iOS自动化登录测试demo
  5. Flutter Widgets 之 SnackBar
  6. 前后端分离下的跨域CAS请求
  7. 【线上测试之后的应用】基于MySQL+MHA+Haproxy构建高可用负载均衡数据库集群(详解)
  8. 斐讯k2
  9. 移动webApp必备技能一、WebApp 里Meta标签大全,webappmeta标签大全
  10. XCTF---easyjni的WriteUp