-
Notifications
You must be signed in to change notification settings - Fork 0
/
LCS.java
49 lines (49 loc) · 1.7 KB
/
LCS.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
import java.util.*;
public class LCS {
public static int lcs(String s1, String s2) {
int n = s1.length();
int m = s2.length();
Queue<Integer> Q = new LinkedList<>();
int matNum = 0;
for(int i = 0; i <= n; i++){
Queue<Integer> cRow = new LinkedList<>();
for(int j = 0; j <= m; j++){
if(i == 0 || j == 0){
cRow.offer(0);
} else{
if(s1.charAt(i - 1) == s2.charAt(j - 1)){
int diag = Q.poll();
int pVal = diag + 1;
cRow.offer(pVal);
if (pVal > matNum){
matNum = pVal;
}
} else{
int diag = Q.poll();
int vertVal = cRow.peek();
int pVal = Math.max(diag, vertVal);
cRow.offer(pVal);
}
}
}
Q = cRow;
}
return matNum;
}
public static void main(String[] args) {
long avgtime=0;
for(int i = 0; i < 1; i++){
String s1 = "qhvtntzofziwrsxgfruozylrzzbrpcuf";
String s2 = "joihojfhntqmupaqchotufcwvijhkqbm";
long startTime = System.nanoTime();
@SuppressWarnings("unused")
int lcsLength = lcs(s1, s2);
long stopTime = System.nanoTime();
System.out.println(lcsLength);
avgtime += (stopTime - startTime);
//System.out.println((stopTime - startTime));
}
float avgF = avgtime;
System.out.println(avgF/1000000);
}
}