国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 開發設計 > 正文

132. Palindrome Partitioning II

2019-11-08 03:21:02
字體:
來源:轉載
供稿:網友

一開始還用上一題的方法一定超時了,沒想到,dp還是難,要2刷和理解 Calculate and maintain 2 DP states: 1. pal[i][j] , which is whether s[i..j] forms a pal 2. 3. d[i], which is the minCut for s[i..n-1] 4. Once we comes to a pal[i][j]==true: ? if j==n-1, the string s[i..n-1] is a Pal, minCut is 0, d[i]=0; ? else: the current cut num (first cut s[i..j] and then cut the rest s[j+1…n-1]) is 1+d[j+1], compare it to the exisiting minCut num d[i], repalce if smaller. d[0] is the answer.

class Solution {public: int minCut(string s) { int n = s.length(); if(n == 0) return 0; vector<vector<bool>>v(n, vector<bool>(n, false)); vector<int>dp(n); for(int i = n - 1; i >= 0; -- i){ dp[i] = n - i - 1; for(int j = i; j < n; ++ j){ if(s[i] == s[j] && (j - i < 2 || v[i + 1][j - 1] == true)){ v[i][j] = true; if(j == n - 1) dp[i] = 0; else if(dp[j + 1] + 1 < dp[i]) dp[i] = dp[j + 1] + 1; } } } return dp[0]; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 桂阳县| 陇西县| 灯塔市| 平武县| 吉林市| 高唐县| 库车县| 临潭县| 芒康县| 焉耆| 屯门区| 磐石市| 莎车县| 炎陵县| 浠水县| 阿城市| 贵南县| 瑞安市| 武邑县| 瓮安县| 吉林省| 漠河县| 汶上县| 枣阳市| 济源市| 红原县| 潮安县| 西平县| 绍兴市| 敖汉旗| 阜康市| 襄樊市| 邯郸市| 内黄县| 曲麻莱县| 郸城县| 利津县| 博罗县| 利津县| 庐江县| 右玉县|