1
1
import java .io .BufferedReader ;
2
2
import java .io .InputStreamReader ;
3
3
import java .io .PrintWriter ;
4
- import java .util .Arrays ;
5
4
6
5
/**
7
6
*
@@ -31,13 +30,38 @@ public static void main(String[] args) throws Exception {
31
30
}
32
31
33
32
void solve () throws Exception {
34
- longest_increasing_subsequence ();
33
+ //longest_increasing_subsequence();
34
+ longest_common_subsequence ();
35
+ }
36
+
37
+ private void longest_common_subsequence () {
38
+ String x = "AGGTAB" ;
39
+ String y = "AGTB" ;
40
+
41
+ pout .println ("LCS: " +lcs (x , y ));
42
+ }
43
+
44
+ int lcs (String input1 ,String input2 ){
45
+ int len1 = input1 .length ();
46
+ int len2 = input2 .length ();
47
+ int dp [][] = new int [len1 +1 ][len2 +1 ];
48
+ for (int i =1 ;i <=len1 ;i ++){
49
+ for (int j =1 ;j <=len2 ;j ++){
50
+ if (input1 .charAt (i -1 )==input2 .charAt (j -1 )){
51
+ dp [i ][j ] = dp [i -1 ][j -1 ] + 1 ;
52
+ }
53
+ else
54
+ dp [i ][j ] = Math .max (dp [i -1 ][j ], dp [i ][j -1 ]);
55
+ }
56
+ }
57
+
58
+ return dp [len1 ][len2 ];
35
59
}
36
60
37
61
private void longest_increasing_subsequence () {
38
62
int arr [] = { 10 , 22 , 9 , 33 , 21 , 50 , 41 , 60 };
39
63
int n = arr .length ;
40
- System . out .println ("Length of lis is "
64
+ pout .println ("Length of lis is "
41
65
+ lis (arr , n ) + "\n " );
42
66
}
43
67
@@ -61,6 +85,7 @@ int lis(int arr[],int n){
61
85
62
86
return max ;
63
87
}
64
-
65
-
88
+
66
89
}
90
+
91
+
0 commit comments