-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPallindrom.cpp
50 lines (46 loc) · 958 Bytes
/
Pallindrom.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
#include <bits/stdc++.h>
using namespace std;
string s;
int palin()
{
int n = s.length();
int l;
vector<vector<bool> > p(n , vector<bool> (n));
vector<int> no(n);
for(int i = 0 ; i < n ; i++)
p[i][i] = 1;
for(int i = 2 ; i <= n ; i++)
{
for(int j =0 ; j <= n-i ; j++)
{
l = j+i-1;
if(i == 2)
p[j][l] = (s[j] == s[l]);
else
p[j][l] = (p[j+1][l-1])&&(s[j] == s[l]);
}
}
for(int i = 0 ; i < n ;i++)
{
if(p[0][i] == 1)
no[i] = 0;
else
{
no[i] = INT_MAX;
for(int j = 0 ; j < i ; j++)
{
if(p[j+1][i] == true && ((1 + no[j]) < no[i]))
no[i] = no[j]+1;
}
}
}
return no[n-1];
}
int main() {
int t;
cin >> t;
while(t--){
cin >> s;
cout << palin();
}
}