-
Notifications
You must be signed in to change notification settings - Fork 34
/
1044-Paliandrome-Partitioning.cpp
96 lines (60 loc) · 1.16 KB
/
1044-Paliandrome-Partitioning.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <iostream>
#include <string.h>
using namespace std;
int palin[1005][1005];
int generate(string x)
{
int n;
int e;
n = x.length();
for (int i = 0; i < n; i++) {
palin[i][i] = 1;
}
for (int length = 2; length <= n; length++) {
for (int s = 0; s <= n - length; s++) {
e = s + length - 1;
if(length == 2) {
palin[s][e] = (x[s] == x[e]);
}
else {
palin[s][e] = ((x[s] == x[e]) and palin[s+1][e-1]);
}
}
}
}
int main()
{
int t;
int explore[1001][1001];
int n;
scanf("%d", &t);
for (int cases = 1; cases <= t; cases++) {
int l;
char inp[1005];
string x;
scanf("%s", inp);
memset(explore, 0, sizeof(explore));
memset(palin, 0, sizeof(palin));
x = inp;
n = x.length();
generate(x);
for (int i = n; i >= 0; i--) {
for (int j = n; j >= i; j--) {
if(j == n) {
explore[i][j] = j - i;
continue;
}
if(palin[i][j]) {
explore[i][j] = min( 1 + explore[j+1] [j+1], explore[i][ j+1]);
continue;
}
else {
explore[i][j] = explore[i][j+1];
continue;
}
}
}
printf("Case %d: %d\n", cases, explore[0][0]);
}
}