-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpractical16.java
57 lines (48 loc) · 1.65 KB
/
practical16.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
54
55
56
57
class Cell{
char direction;
int count;
Cell(char direction, int count){
this.direction=direction;
this.count = count;
}
}
public class practical16 {
public static void getString(Cell[][] matrix, String s1, int i, int j){
if(i==0 || j==0){
return;
} else if(matrix[i][j].direction == 'd'){
getString(matrix, s1, i-1,j-1);
System.out.print(s1.charAt(i-1));
} else if(matrix[i][j].direction == 'u'){
getString(matrix, s1, i-1,j);
} else{
getString(matrix, s1, i, j-1);
}
}
public static void LCS(String s1, String s2){
Cell[][] matrix = new Cell[s1.length()+1][s2.length()+1];
for(int i=0; i<=s1.length(); i++){
for(int j=0; j<=s2.length(); j++){
if(i==0 || j==0){
matrix[i][j] = new Cell('o', 0);
} else if(Character.toLowerCase(s1.charAt(i-1)) == Character.toLowerCase(s2.charAt(j-1))){
matrix[i][j] = new Cell('d', matrix[i-1][j-1].count+1);
} else{
int max = matrix[i-1][j].count;
if(max > matrix[i][j-1].count){
matrix[i][j] = new Cell('u', max);
} else{
max = matrix[i][j-1].count;
matrix[i][j] = new Cell('l', max);
}
}
}
}
getString(matrix, s1, s1.length(), s2.length());
}
public static void main(String[] args) {
String str1 = "providence";
String str2 = "president";
LCS(str1, str2);
}
}