-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path132.cpp
More file actions
51 lines (46 loc) · 1.19 KB
/
132.cpp
File metadata and controls
51 lines (46 loc) · 1.19 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
//
// 132.cpp
// LeetCode
//
// Created by 张佐玮 on 15/7/17.
// Copyright (c) 2015年 JarvisZhang. All rights reserved.
//
// Title: Palindrome Partitioning II
//
#include <iostream>
#include <vector>
using namespace std;
class Solution {
public:
int minCut(string s) {
int length = (int) s.size();
// vector<vector<bool>> isPalindrom(length, vector<bool>(length, false));
bool isPalindrom[length][length];
int cut[length];
// vector<int> cut(length);
for (int i = 0; i < length; i++) {
cut[i] = length - i - 1;
}
for (int i = length - 1; i >= 0; i--) {
for (int j = i; j < length; j++) {
isPalindrom[i][j] = (i == j) || ((i == j - 1 || isPalindrom[i+1][j-1]) && s[i] == s[j]);
if (isPalindrom[i][j]) {
cut[i] = min((j == length - 1 ? 0 : cut[j+1] + 1), cut[i]);
}
}
}
return cut[0];
}
};
class Test {
private:
static void runTest(string s) {
Solution solution;
cout << solution.minCut(s) << endl;
}
public:
void sample() {
string s1("aaabaa");
runTest(s1);
}
};