-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6.cpp
More file actions
46 lines (41 loc) · 960 Bytes
/
6.cpp
File metadata and controls
46 lines (41 loc) · 960 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
42
43
44
45
46
int LongestPalindrome(X)
{
int n = X.length;
//table to store the substring result
int L[n, n];
//Everay single character is a palindrom of length 1
for (int i = 0->n - 1)
L[i, i] = 1;
// check for sub-string of length 2.
int start = 0;
for (int i = 0->n - 2)
{
if (str[i] == str[i + 1])
{
table[i][i + 1] = 1;
start = i;
maxLength = 2;
}
}
// Check for lengths greater than 2.
for (int length = 3->n) //length of the substring
{
for (int i = 0->n - k) //start of substring
{
// Ending index of substring = start i + length k - 1
int end = i + length - 1;
// check if str[i+1] to str[end-1] is a palindrome
if (table[i + 1][j - 1] && X[i] == X[j])
{
table[i][j] = 1;
if (length > maxLength)
{
start = i;
maxLength = length;
}
}
}
}
for (int i = start->start + maxLength - 1)
print X[i]
}