-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlongest_pallindromic_sequence.java
53 lines (50 loc) · 1.18 KB
/
longest_pallindromic_sequence.java
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
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/**
*
* @author shivam
*/
public class longest_pallindromic_sequence {
static int max(int a,int b)
{
return a>b?a:b;
}
public static void main(String[] args) {
String s="agbdba";
int len=s.length(),i,j,l;
int t[][]= new int[len][len];
for(i=0;i<len;i++)
{
t[i][i]=1;
}
for(l=2;l<=len;l++)
{
for(i=0;i<s.length()-l+1;i++)
{
j=i+l-1;
System.out.println(i+":"+j);
if(s.charAt(i)==s.charAt(j))
{
t[i][j]=2+t[i+1][j-1];
}
else
{
t[i][j]=max(t[i][j-1],t[i+1][j]);
}
}
}
System.out.println("dp array is ");
for(i=0;i<len;i++)
{
for(j=0;j<len;j++)
{
System.out.print(t[i][j]+" ");
}
System.out.println("");
}
System.out.println("max value that can be obtained is "+t[0][len-1]);
}
}