leetcode -- Palindrome Partitioning II

本文介绍了一种算法,用于计算将字符串分割成多个回文子串所需的最少切割次数。通过动态规划的方法,预先计算字符串各部分是否为回文,并在此基础上递推求解最小分割数。

指责别人,看清自己

[问题描述]

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.

[解题思路]

ans[i]表示s[i]至最后需要的划分数

注意:line6 "<="

 1 int Solution::minCut(string s){
 2     const int len = s.length();
 3     bool pali[len][len];
 4     int ans[len];
 5     fill_n(&pali[0][0], len*len, false);
 6     for (int i = 0; i <= len; i++)
 7         ans[i] = len - i - 1;
 8     for (int i = len - 1; i >= 0; i --){
 9         for (int j = i; j < len; j ++){
10             pali[i][j] = s[i]==s[j]&&(j-i<2||pali[i+1][j-1]);
11             pali[i][j] == true?(ans[i] = min(ans[i], ans[j + 1] + 1)):(true);
12         }
13     }
14     return ans[0];
15 }

 

转载于:https://www.cnblogs.com/taizy/p/4008836.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值