forked from illuz/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTLE_recursive_2^n.cpp
More file actions
42 lines (37 loc) · 864 Bytes
/
Copy pathTLE_recursive_2^n.cpp
File metadata and controls
42 lines (37 loc) · 864 Bytes
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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: TLE_recursive_2^n.cpp
* Create Date: 2015-03-19 10:10:55
* Descripton:
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
bool isPalindr(string s) {
int len = s.length();
for (int i = 0; i < len; ++i)
if (s[i] != s[len - i - 1])
return false;
return true;
}
public:
int minCut(string s) {
int len = s.length();
if (isPalindr(s))
return 0;
int cut = len - 1;
for (int i = 1; i < len; ++i)
if (isPalindr(s.substr(0, i)))
cut = min(cut, 1 + minCut(s.substr(i)));
return cut;
}
};
int main() {
string str;
Solution s;
while (cin >> str)
cout << s.minCut(str) << endl;
return 0;
}