【DP】回文的最小分割数
来源:互联网 发布:幼儿园网络研修的简报 编辑:程序博客网 时间:2024/06/04 19:30
题目描述
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab",
Given a string s, partition s such that every substring of the partition is a palindrome.
Return the minimum cuts needed for a palindrome partitioning of s.
For example, given s = "aab",
Return 1 since the palindrome partitioning ["aa","b"] could be produced using 1 cut.
动态规划总是练不好,强行递归每次又都超时,真的是烦。
思路:从头开始,第一个元素单独切割一定是回文。
dp[i]表示以第i个字符结尾分割成回文组的最小分割数。
isParli[i][j]表示从第i个字符到第j个字符能否构成回文。
isParli[i][j] = ((charAt(i) == charAt(k) ) && (i - k < 2 || isParli[i - 1][k + 1]));
return最后一个dp[]即为结果。
public class Solution { public int minCut(String s) { int [] dp = new int [s.length() + 1]; boolean [][] isParli = new boolean [s.length() + 1][s.length() + 1]; dp[0] = -1; for(int i = 2; i <= s.length(); ++i) dp[i] = Integer.MAX_VALUE; for(int i = 1; i <= s.length(); ++i){ for(int k = 1; k <= i; ++k){ if((i - k < 2 || isParli[i - 1][k + 1]) && s.charAt(i - 1) == s.charAt(k - 1)){ isParli[i][k] = true; dp[i] = Math.min(dp[i],dp[k - 1] + 1); } } } return dp[s.length()]; } }
阅读全文
0 0
- 【DP】回文的最小分割数
- 【DP】回文的最小分割数2
- 最小回文分割数 Palindrome Partitioning II
- 字符串问题---回文最小分割数
- 动态规划之回文最小分割数
- 分割成回文需要的最小分割数 Palindrome Partitioning II @LeetCode
- 求回文字符串的最小分割
- LeetCode-----回文串的最小分割
- 回文数、整数输出,数字序列最小分割问题
- 动态规划——回文串最小分割数
- 大于一个数的最小回文数
- 回文最少分割数
- 回文最少分割数
- 回文最少分割数
- 【DP生成回文字符串的最小插入字符数】POJ 1159
- [LeetCode]—Palindrome Partitioning II 回文分割,求最小分割数
- 构成最小回文数的分析
- 大于N的最小回文数
- RecyclerView的Item监听
- C#基础-006(1)if练习题 判断一个数是不是偶数,如果是偶数那么就加1并打印
- url 映射 3种匹配方法
- ImageLoader获取磁盘中存入的图片
- L
- 【DP】回文的最小分割数
- poj1852 Ants
- C 语言各个数据类型所占字节数
- oracle中like总结
- poj3617 Best Cow Line
- IIS WebService 内存溢出问题的修改过程
- 【leetcode】38. Count and Say(Python & C++)
- 找出1000以内的完全数。
- spring boot 开发热部署配置 (idea IDE)