-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathcode_2.cpp
45 lines (39 loc) · 934 Bytes
/
code_2.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
//
// Algorithm
//
// Created by Mohd Shoaib Rayeen on 23/11/18.
// Copyright © 2018 Shoaib Rayeen. All rights reserved.
//
#include <iostream>
#include <string.h>
using namespace std;
int lps(string X) {
long n = X.length();
int table[n][n];
for (int i = 0; i < n; i++) {
table[i][i] = 1;
}
int j;
for (int cl=2; cl<=n; cl++) {
for (int i=0; i<n-cl+1; i++) {
j = i+cl-1;
if (X[i] == X[j] && cl == 2) {
table[i][j] = 2;
}
else if (X[i] == X[j]) {
table[i][j] = table[i+1][j-1] + 2;
}
else {
table[i][j] = max(table[i][j-1], table[i+1][j]);
}
}
}
return table[0][n-1];
}
int main() {
string X;
cout << "\nEnter String\t\t:\t";
getline(cin , X);
cout << "\nLength of LPS\t\t:\t" << lps(X) << endl;
return 0;
}