GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — leetcode/Palindrome PartitioningII.txt at master · handongfeng/leetcode Expand file tree/ Copy pathPalindrome PartitioningII.txtMore file actions 90 lines (81 loc) · 1.36 KB / Copy pathPalindrome PartitioningII.txtFile metadata and controls90 lines (81 loc) · 1.36 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 class Solution { public: void palindrome(string s,vector<vector<bool>>& m) { int n = s.size(); m.clear(); if (n<=0) return ; //vector<vector<bool>> m; // init for(int i=0;i<n;i++) { vector<bool> v(n,false); m.push_back(v); } for(int j=0;j<n;j++) for(int i=0;i<=j;i++) { int length = j-i+1; if(length <=0) m[i][j]=false; if(length ==1) m[i][j]=true; if(length ==2) { if(s[i]==s[j]) m[i][j]=true; else m[i][j]=false; } if(length>2) { m[i][j] = ( (s[i]==s[j]) && m[i+1][j-1] ); } } } int dp(string s) { vector<vector<bool>> c; palindrome(s,c); vector<vector<int>> m; int n= s.size(); m.resize(n); for(int i=0;i<n;i++) { m[i].resize(n); for(int j=0;j<n;j++) m[i][j] =0; } for(int j=0;j<n;j++) { for(int i=0;i<=j;i++) { int length = j-i+1; if(length==1) { m[i][j] = 0; } else { int _min = 1000000000; for(int k=i;k<=j-1;k++) { //if(c[i][k] && c[k+1][j]) { _min = min(_min, m[i][k] + m[k+1][j] + 1); } } if(c[i][j]) { m[i][j] = 0; }else m[i][j] =_min; } } } return m[0][n-1]; } int minCut(string s) { // Start typing your C/C++ solution below // DO NOT write int main() function return dp(s); } }; |
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |