-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMinimumCutOfPalindrom.cpp
72 lines (65 loc) · 1.43 KB
/
MinimumCutOfPalindrom.cpp
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
/*Minimum cut of palindrom*/
#include <iostream>
#include <vector>
using namespace std;
int MinimumCutOfPalindrom(char* src){
if(src == NULL)
return -1;
unsigned int length = 0;
while(*(src+length) != '\0')
length ++;
cout<<length<<endl;
int IsCut[length];
//memset(IsCut, -1, length); //memset is not set
for(int i=0; i< length; i++)
IsCut[i] = -1;
IsCut[0] = 1;
int pos = 1;
while(pos < length){
//odd symmetric
int i;
for(i=1; i <= pos && (pos+i) < length; i++){
if(src[pos-i] != src[pos+i])
break;
IsCut[pos-i] = 0;
IsCut[pos+i] = 0;
}
if(i > 1 && i <= pos){
IsCut[pos-i] = 1;
IsCut[pos] = 0;
pos = pos + i;
}else if (i > 1){
IsCut[pos] = 0;
pos = pos+i;
continue;
}
//even symmetric
int left = pos- 1 ;
int right = pos;
for(; left >= 0 && right < length && src[left] == src[right]; left--, right++){
IsCut[left] = 0;
IsCut[right] = 0;
}
if(left >= 0)
IsCut[left] = 1;
if(right < length && pos < right)
IsCut[right - 1] = 1;
else if(pos == right)
IsCut[pos] = 1;
pos = (pos == right) ? pos+1 : right;
}
int MinCut = 0;
for(int i = 0; i< length -1; i++){
if(IsCut[i] == 1)
MinCut ++;
//if(IsCut[i] == -1)
cout<<i<<" "<<IsCut[i]<<" ";
}
cout<<endl;
return MinCut;
}
int main(){
char src[1256];
cin>>src;
cout<<MinimumCutOfPalindrom(src)<<endl;
}