Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 100
  5.  
  6. int max(int a, int b);
  7. /* Utility function to get max of 2 integers */
  8. int max(int a, int b)
  9. {
  10. if (a>b){
  11. return a;
  12. }
  13. else {
  14. return b;
  15. }
  16. }
  17. int main(int argc, char* argv[ ]){
  18. FILE *arq1;
  19. FILE *arq2;
  20.  
  21. arq1 = fopen(argv[1],"r");
  22. arq2 = fopen(argv[2],"r");
  23. if (arq1 == NULL){
  24. printf("!!!!ERROR!!!!\n");
  25. exit;
  26. }
  27. if (arq2 == NULL){
  28. printf("!!ERROR!!\n");
  29. exit;
  30. }
  31. char palavra[MAX];
  32. char vetor[MAX][MAX];
  33. char palavra2[MAX];
  34. char vetor2[MAX][MAX];
  35. int p = 0,k = 0;
  36.  
  37. setbuf(stdin, NULL);
  38.  
  39. while (fgets(palavra, MAX, arq1)){
  40. strcpy(vetor[p], palavra);
  41. p++;
  42. }
  43. while (fgets(palavra2, MAX, arq2)){
  44. strcpy(vetor2[k], palavra2);
  45. k++;
  46. }
  47. fclose(arq1);
  48. fclose(arq2);
  49. //printf("%d %d \n ", p, k);
  50.  
  51.  
  52. int L[p+1][k+1];
  53.  
  54. /* Following steps build L[m+1][n+1] in bottom up fashion. Note
  55. that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
  56. for (int i=0; i<=p; i++)
  57. {
  58. for (int j=0; j<=k; j++)
  59. {
  60. if (i == 0 || j == 0)
  61. L[i][j] = 0;
  62. else if (strcmp(vetor[i-1],vetor2[j-1])== 0)
  63. L[i][j] = L[i-1][j-1] + 1;
  64. else
  65. L[i][j] = max(L[i-1][j], L[i][j-1]);
  66. }
  67. }
  68.  
  69. // Following code is used to print LCS
  70. int index = L[p][k];
  71.  
  72. // Create a character array to store the lcs string
  73. char lcs[index+1][index+1];
  74. lcs[index][index] = '\0'; // Set the terminating character
  75.  
  76. // Start from the right-most-bottom-most corner and
  77. // one by one store characters in lcs[]
  78. int i = p, j = k;
  79. while (i > 0 && j > 0)
  80. {
  81. // If current character in X[] and Y are same, then
  82. // current character is part of LCS
  83. if (strcmp(vetor[i-1],vetor2[j-1])==0)
  84. {
  85. strcpy(lcs[index-1],vetor[i-1]); // Put current character in result
  86. i--;
  87. j--;
  88. index--; // reduce values of i, j and index
  89. }
  90.  
  91. // If not same, then find the larger of two and
  92. // go in the direction of larger value
  93. else if (L[i-1][j] > L[i][j-1])
  94. i--;
  95. else
  96. j--;
  97.  
  98. }
  99. printf("%d\n", index);
  100. // for(int t=0; t<=index; t++){
  101. // printf("%s",lcs[t]);
  102. // }
  103.  
  104.  
  105.  
  106.  
  107. //lerarquivostring(arquivo1, arquivo2);
  108.  
  109. //chamar função pra ler arquivo
  110. //retonar os arquivos lidos
  111. //chamar função pra contar o tamanho das linhas
  112. //alocar dinamicamente o tamanho das linhas
  113. //colocar cada linha em uma posição do vetor
  114. //aplicar lcs
  115. //gerar a resposta
  116. //dar free
  117. //fechar o arquivo
  118. return 0;
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement