国产探花免费观看_亚洲丰满少妇自慰呻吟_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]; }};
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 方山县| 黄骅市| 乌恰县| 康乐县| 沾益县| 滨海县| 抚顺县| 平原县| 宿松县| 吉水县| 河曲县| 平阳县| 南部县| 叶城县| 磐安县| 志丹县| 罗平县| 乳山市| 连城县| 克什克腾旗| 方正县| 宝山区| 扎赉特旗| 饶阳县| 山阳县| 饶河县| 永嘉县| 池州市| 宣恩县| 思南县| 湘西| 辽中县| 临江市| 密山市| 合水县| 山东省| 万州区| 湛江市| 通州区| 南昌县| 中西区|