Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.50 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #define MAX 300
  5.  
  6.  
  7. int max(int a, int b);
  8. int max(int a, int b)
  9. {
  10. if (a>b){
  11. return a;
  12. }
  13. else {
  14. return b;
  15. }
  16. }
  17.  
  18. //--------- ALOCAR O TAMANHO DOS ARQUIVOS-----------//
  19.  
  20. /*int** aloc_int_lcs(int i, int j)
  21. {
  22. int** aloc_int;
  23. int aux1,aux2;
  24.  
  25. aloc_int = (int**)calloc(i,sizeof(int*));
  26. for(aux1 = 0; aux1 < i; aux1++)
  27. {
  28. aloc_int[aux1] = (int*)calloc(i,sizeof(int));
  29. for (aux2 = 0; aux2 < j; aux2++)
  30. {
  31. aloc_int[aux1][aux2] = 0;
  32. }
  33. }
  34. if (aloc_int == NULL)
  35. {
  36. printf("Houve um erro\n");
  37. }
  38.  
  39. for (int h = 0; h<i;h++){
  40. for(int g = 0; g<j;g++){
  41. printf("%p\n",&aloc_int[h][g]);}
  42. }
  43.  
  44.  
  45. return aloc_int;
  46. }*/
  47.  
  48.  
  49.  
  50. //-------------------- FREE ----------------------//
  51. void free_aloc(char** aloc_char1, int tam_int1)
  52. {
  53. for(int aux3 = 0; aux3 < tam_int1; aux3++)
  54. {
  55. free(aloc_char1[aux3]);
  56. }
  57. free(aloc_char1);
  58. }
  59.  
  60. //-------------- ABRIR ARQUIVO -------------------//
  61. void abrirarquivo(FILE *arq, int* tamanho, char** arquivo)
  62. {
  63. if (arq == NULL)
  64. {
  65. printf("!!!!ERROR!!!!\n");
  66. exit;
  67. }
  68. setbuf(stdin, NULL);
  69. char ln_arc[MAX];
  70. int aux1;
  71. int ln = 0;
  72. while (fgets(ln_arc, MAX, arq))
  73. {
  74. aux1 = strlen(ln_arc);
  75. arquivo[ln] = (char*)calloc(aux1+1,sizeof(char));
  76. strcpy(arquivo[ln], ln_arc);
  77. //printf("%s",arquivo[ln]);
  78. ln++;
  79. }
  80. *tamanho = ln;
  81.  
  82. }
  83.  
  84.  
  85. void contlinha(FILE *arquivo, int* aux1)
  86. {
  87. int ln = 0;
  88. if (arquivo == NULL)
  89. {
  90. printf("!!!!ERROR!!!!\n");
  91. exit;
  92. }
  93. char ln_arc[MAX];
  94.  
  95.  
  96. while (fgets(ln_arc, MAX, arquivo))
  97. {
  98. ln++;
  99. }
  100. *aux1 = ln;
  101. }
  102.  
  103.  
  104.  
  105. //--------------- LCS -------------------//
  106.  
  107. char** LCS(char** vetor1, char** vetor2, int p, int k,int* ind_final)
  108. {
  109. int L[p+1][k+1];
  110. for (int i=0; i< p+1; i++)
  111. {
  112. for (int j=0; j< k+1; j++)
  113. {
  114. if (i == 0 || j == 0)
  115. {
  116. L[i][j] = 0;
  117. }
  118. else if (strcmp(vetor1[i-1],vetor2[j-1]) == 0)
  119. {
  120. L[i][j] = L[i-1][j-1] + 1;
  121. }
  122. else
  123. {
  124. L[i][j] = max(L[i-1][j], L[i][j-1]);
  125. }
  126. }
  127. }
  128. /*for(int h = 0; h<p+1;h++){
  129. for(int e = 0; e<k+1;e++){
  130. printf(" %2d ",L[h][e]);
  131. }
  132. printf("\n");
  133. }*/
  134. int index = L[p][k];
  135. *ind_final = index;
  136. char** lcs;
  137. int i = p;
  138. int j = k;
  139. int aux;
  140. lcs = (char**)calloc(p,sizeof(char*));
  141. while (i > 0 && j > 0)
  142. {
  143. if (strcmp(vetor1[i-1],vetor2[j-1]) == 0)
  144. {
  145. aux = strlen(vetor1[i-1]);
  146. lcs[index-1] = (char*)calloc(aux+1,sizeof(char));
  147. strcpy(lcs[index-1],vetor1[i-1]);
  148. //printf("%s",lcs[index-1]);
  149. i--;
  150. j--;
  151. index--;
  152. }
  153. else if (L[i-1][j] > L[i][j-1])
  154. {
  155. i--;
  156. }
  157. else
  158. {
  159. j--;
  160. }
  161. }
  162. /*for (int i=0; i < L[p][k]; i++)
  163. {
  164. printf("%s",lcs[i]);
  165. }*/
  166. //precisa desalocar o lcs;
  167. return lcs;
  168. }
  169.  
  170.  
  171.  
  172. int main(int argc, char* argv[ ])
  173. {
  174. char** string1;
  175. char** string2;
  176. char** matriz_char;
  177. int aux1,aux2;
  178. int tamanho1 = 0, tamanho2 = 0, index = 0;
  179. int arc_1 , arc_2;
  180.  
  181. FILE *arq1;
  182. FILE *arq2;
  183. arq1 = fopen(argv[1],"r");
  184. arq2 = fopen(argv[2],"r");
  185. //contlinha(arq1, &arc_1);
  186. //contlinha(arq2, &arc_2);
  187. //printf("%d %d\n",arc_1,arc_2);
  188.  
  189. string1 = (char**)calloc(26,sizeof(char*));//corrigir pois esta estatico;
  190. abrirarquivo(arq1, &tamanho1,string1);
  191. string2 = (char**)calloc(34,sizeof(char*));//corrigir pois esta estatico;
  192. abrirarquivo(arq2, &tamanho2,string2);
  193. matriz_char = LCS(string1,string2,tamanho1,tamanho2,&index);
  194. //printf("%d\n",index);
  195. for (int i=0; i < index; i++)
  196. {
  197. printf("%s",matriz_char[i]);
  198. }
  199. //printf("%s",matriz_char[7]);
  200. fclose(arq1);
  201. fclose(arq2);
  202.  
  203. free_aloc(string1,tamanho1);
  204. free_aloc(string2,tamanho2);
  205.  
  206. //fprintf(stderr,"João me da dois pontos.\n");
  207.  
  208.  
  209. return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement