代码改变世界

leetcode - Palindrome Partitioning II

2013-12-14 09:38  张汉生  阅读(165)  评论(0编辑  收藏  举报

 

 1 class Solution {
 2 public:
 3     bool isPal(const char * p, const char * q){
 4         while (p<q){
 5             if (*p != *q)
 6                 return false;
 7             p++; q--;
 8         }
 9         return true;
10     }
11     int minCut(string s) {
12         int n = s.length();
13         if (n<=0)
14             return 0;
15         const char * head = s.c_str();
16         const char * end = head + n-1;
17         const char * p = head;
18         vector<int> ePos(n+1, -1);
19         while ((*p) != '\0'){
20             const char * q = head;
21             int curMax = 1000000000;
22             while (q<=p){
23                 if (curMax<=1+ePos[q-head] || !isPal(q,p)){
24                     q++;
25                     continue;
26                 }
27                 curMax = 1 + ePos[q-head];
28                 q++;
29             }
30             ePos[p-head+1] = curMax;
31             p++;
32         }
33         int rlt = ePos[n];
34         return rlt;
35     }
36 };