-
-
Save KannarFr/5e8fb37f5dbb744e0c2413d6d3832e31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Main { | |
final static char[] x = {'A','C','G','T','C','G','A','C','G'}; | |
final static char[] y = {'A','C','T','C','A','C','G'}; | |
final static int INS = -1; | |
final static int DEL = -1; | |
public static void main(String[] args) { | |
int n = y.length; | |
int p = x.length; | |
int[][] table = new int[n + 1][p + 1]; | |
printTable(table); | |
System.out.println(); | |
smithWaterman(table); | |
printTable(table); | |
} | |
private static void printTable(int[][] t) { | |
for (int i=1; i<t.length; i++) { | |
for (int j=1; j<t[i].length; j++) { | |
System.out.print(t[i][j] + " "); | |
} | |
System.out.println(); | |
} | |
} | |
private static void smithWaterman(int[][] t) { | |
t[0][0] = 0; | |
for (int i = 1; i < t.length; i++) { | |
for (int j = 1; j < t[i].length; j++) { | |
t[i][0] = 0; | |
t[0][j] = 0; | |
t[i][j] = max( | |
t[i-1][j-1] + subs(x[j-1], y[i-1]), | |
t[i-1][j] + DEL, | |
t[i][j-1] + INS, | |
0 | |
); | |
} | |
} | |
} | |
private static int subs(char a, char b) { | |
return a == b ? 1 : 0; | |
} | |
private static int max(int a, int b, int c, int d) { | |
return Math.max(Math.max(Math.max(a, b), c), d); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment