Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. public class CadeiaComumMaisLonga {
  2.  
  3. public static int calcularLCS(char [] A, char [] B){
  4. int [][]LCS = new int [A.length+1][B.length+1];
  5.  
  6. for(int i=0;i<=B.length;i++){
  7. LCS[0][i]=0;
  8. }
  9.  
  10. for(int i=0;i<=A.length;i++){
  11. LCS[i][0]=0;
  12. }
  13.  
  14. int maxCadeia = 0;
  15.  
  16. for(int i=1;i<=A.length;i++){
  17. for(int j=1;j<=B.length;j++){
  18. if(A[i-1]==B[j-1]){
  19. LCS[i][j] = LCS[i-1][j-1] +1;
  20. }else{
  21. if(LCS[i-1][j] >= LCS[i][j-1]){
  22. LCS[i][j] = LCS[i-1][j];
  23. }else{
  24. LCS[i][j] = LCS[i][j-1];
  25. }
  26. }
  27.  
  28. if(maxCadeia<LCS[i][j]){
  29. maxCadeia = LCS[i][j];
  30. }
  31.  
  32. System.out.print(LCS[i][j]);
  33. }
  34. System.out.println();
  35. }
  36.  
  37. return maxCadeia;
  38. }
  39. public static void main(String[] args) {
  40. String A = "ACCGGTCGAG";
  41. String B = "GTCGTTCGGAATGCCGTTGCTCTGT";
  42. System.out.println("Cadeia comum mais longa : " + calcularLCS(A.toCharArray(), B.toCharArray()));
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement