Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.68 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 300
  5.  
  6. int max(int a, int b);
  7. int max(int a, int b)
  8. {
  9. if (a>b){
  10. return a;
  11. }
  12. else {
  13. return b;
  14. }
  15. }
  16. int main(int argc, char* argv[ ]){
  17. FILE *arq1;
  18. FILE *arq2;
  19.  
  20. arq1 = fopen(argv[1],"r");
  21. arq2 = fopen(argv[2],"r");
  22. if (arq1 == NULL){
  23. printf("!!!!ERROR!!!!\n");
  24. exit;
  25. }
  26. if (arq2 == NULL){
  27. printf("!!ERROR!!\n");
  28. exit;
  29. }
  30. char palavra[MAX];
  31. char vetor[MAX][MAX];
  32. char palavra2[MAX];
  33. char vetor2[MAX][MAX];
  34. int p = 0,k = 0;
  35.  
  36. setbuf(stdin, NULL);
  37.  
  38. while (fgets(palavra, MAX, arq1)){
  39. strcpy(vetor[p], palavra);
  40. p++;
  41. }
  42. while (fgets(palavra2, MAX, arq2)){
  43. strcpy(vetor2[k], palavra2);
  44. k++;
  45. }
  46. fclose(arq1);
  47. fclose(arq2);
  48. //printf("%d %d\n", p, k);
  49.  
  50.  
  51. int **aloc_int;
  52. aloc_int = (int**)calloc(p,sizeof(int*));
  53. for(int h = 0; h<p; h++){
  54. aloc_int[h] = (int*)calloc(k,sizeof(int));
  55. }
  56.  
  57. if (aloc_int == NULL){
  58. printf("Houve um erro\n");
  59. }
  60. printf("Sem erros no aloc\n");
  61.  
  62.  
  63. int L[p][k];
  64.  
  65. for (int i=0; i<=p; i++) {
  66. for (int j=0; j<=k; j++) {
  67. if (i == 0 || j == 0) {
  68. L[i][j] = 0; }
  69. else if (strcmp(vetor[i],vetor2[j])== 0){
  70. L[i][j] = L[i-1][j-1] + 1;
  71. }
  72. else{
  73. L[i][j] = max(L[i-1][j], L[i][j-1]); }
  74. }
  75. }
  76. // for(int q = 0; q < p+1; q++){
  77. // for (int r=0; r < k+1; r++){
  78. // printf(" %2d ",L[q][r]);
  79. // }
  80. // printf("\n");
  81. // }
  82. int index = L[p][k];
  83. printf("O index é %d\n",index);
  84.  
  85. char lcs[index][index];
  86.  
  87. int i = p;
  88. int j = k;
  89.  
  90. while (i > 0 && j > 0){
  91.  
  92. if (L[i-1] == L[j-1]){
  93. // printf("So testando %d\n",i);
  94. strcpy(lcs[index],vetor[i]);
  95. printf("%s",lcs[index]);
  96. i--;
  97. j--;
  98. index--;
  99. }
  100. else if (L[i-1][j] > L[i][j-1]){
  101. i--;
  102. }
  103. else{
  104.  
  105. j--;
  106. }
  107. }
  108. //printf("%s",lcs[index]);
  109. for(int h =0; h<p; h++){
  110. free(aloc_int[h]);
  111. }
  112. free(aloc_int);
  113.  
  114.  
  115.  
  116. //lerarquivostring(arquivo1, arquivo2);
  117.  
  118. //chamar função pra ler arquivo
  119. //retonar os arquivos lidos
  120. //chamar função pra contar o tamanho das linhas
  121. //alocar dinamicamente o tamanho das linhas
  122. //colocar cada linha em uma posição do vetor
  123. //aplicar lcs
  124. //gerar a resposta
  125. //dar free
  126. //fechar o arquivo
  127. printf("Fim do programa.\n");
  128. return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement