[LeetCode] Palindrome Partitioning II
来源:互联网 发布:人工智能在线观看土豆 编辑:程序博客网 时间:2024/06/05 04:07
Use DFS will result in TLE!
DP: dp[i] denotes the minimum number of palindromic substrings from i->n, isPal[i][j] denotes if string(i,j) is palindromic.
Thus we have:
isPal[i][j] = (s[i]==s[j] && isPal[i+1][j-1]);
dp[i] = min(dp[j]+1), i<j<n.
class Solution {public: int minCut(string s) { int len = s.size(); int* dp = new int[len+1]; for(int i=len; i>=0; i--) dp[i] = len-i; bool** isPal = new bool*[len]; for(int i=0; i<len; i++) { isPal[i] = new bool[len]; memset(isPal[i], false, sizeof(bool)*len); } for(int i=len-1; i>=0; i--) for(int j=i; j<len; j++) if(s[i] == s[j] && (j-i<2 || isPal[i+1][j-1])) { isPal[i][j] = true; dp[i] = min(dp[i], dp[j+1]+1); } return dp[0]-1; }};
0 0
- LeetCode : Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- leetcode:Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- leetcode - Palindrome Partitioning II
- [LeetCode]Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- leetcode Palindrome Partitioning II
- LeetCode-Palindrome Partitioning II
- [leetcode] Palindrome Partitioning II
- LeetCode - Palindrome Partitioning II
- [Leetcode]Palindrome Partitioning II
- 【leetcode】Palindrome Partitioning II
- LeetCode Palindrome Partitioning II
- [leetcode]Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- [LeetCode] Palindrome Partitioning II
- 构造函数 ;构造函数的执行顺序。静态字段的初始化顺序。new关键字为我们做了什么。
- Python语言获取目录下所有文件或目录的方法
- Python——列表,元组
- vlc编译:用GDB调试vlc
- sdjzuoj——1011:软件版本
- [LeetCode] Palindrome Partitioning II
- ANDROID正在向工业领域渗透(视频为证)
- Tian Ji -- The Horse Racing
- vlc的应用:用vlc做单播,组播及点播服务器
- Java8中的时间和日期
- QT内置的窗口部件类和对话框类
- C++截屏工具开发技术
- hql语句
- 『算法学习笔记』10th day. 文件操作<1>