-
Notifications
You must be signed in to change notification settings - Fork 12
/
all_palindrome_substring.cpp
63 lines (53 loc) · 1.66 KB
/
all_palindrome_substring.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
// C++ program to find palindromic substrings of a string
// Returns total number of palindrome substring of
// length greater then equal to 2
int CountPS(char str[], int n)
{
// create empty 2-D matrix that counts all palindrome
// substring. dp[i][j] stores counts of palindromic
// substrings in st[i..j]
int dp[n][n];
memset(dp, 0, sizeof(dp));
// P[i][j] = true if substring str[i..j] is palindrome,
// else false
bool P[n][n];
memset(P, false , sizeof(P));
// palindrome of single length
for (int i= 0; i< n; i++)
P[i][i] = true;
// palindrome of length 2
for (int i=0; i<n-1; i++)
{
if (str[i] == str[i+1])
{
P[i][i+1] = true;
dp[i][i+1] = 1 ;
}
}
// Palindromes of length more than 2. This loop is similar
// to Matrix Chain Multiplication. We start with a gap of
// length 2 and fill the DP table in a way that gap between
// starting and ending indexes increases one by one by
// outer loop.
for (int gap=2 ; gap<n; gap++)
{
// Pick starting point for current gap
for (int i=0; i<n-gap; i++)
{
// Set ending point
int j = gap + i;
// If current string is palindrome
if (str[i] == str[j] && P[i+1][j-1] )
P[i][j] = true;
// Add current palindrome substring ( + 1)
// and rest palindrome substring (dp[i][j-1] + dp[i+1][j])
// remove common palindrome substrings (- dp[i+1][j-1])
if (P[i][j] == true)
dp[i][j] = dp[i][j-1] + dp[i+1][j] + 1 - dp[i+1][j-1];
else
dp[i][j] = dp[i][j-1] + dp[i+1][j] - dp[i+1][j-1];
}
}
// return total palindromic substrings
return dp[0][n-1];
}