Advertisement
Guest User

Untitled

a guest
Nov 15th, 2019
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.43 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.  
  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 nova[p+1][k+1];
  53.  
  54.  
  55. for (int i=0; i<=p; i++) {
  56. for (int j=0; j<=k; j++) {
  57. if (i == 0 || j == 0) {
  58. nova[i][j] = 0; }
  59. else if (strcmp(vetor[i-1],vetor2[j-1])== 0){
  60. nova[i][j] = nova[i-1][j-1] + 1;
  61. }
  62. else{
  63. nova[i][j] = max(nova[i-1][j], nova[i][j-1]); }
  64. }
  65. }
  66. /*for (i=0; i<=p; i++) {
  67. for (j=0; j<=k; j++) {
  68. printf(" %2d ", nova[i][j]);
  69. }
  70. printf("\n");
  71. }*/
  72.  
  73. int index = nova[p][k];
  74.  
  75.  
  76. char lcs[index+1][0];
  77. //printf("%d\n",index+1);
  78. lcs[index][0] = '\0';
  79.  
  80.  
  81. int i = p;
  82. int j = k;
  83. //printf("Passei aqui 1.\n");
  84. //printf("%s", vetor[2]);
  85. while (i > 0 && j > 0){
  86.  
  87.  
  88. if (nova[i-1][j] == nova[i][j-1]){
  89. strcpy(lcs[index-1],vetor[i-1])
  90. i--;
  91. j--;
  92. index--;
  93. }
  94. else if (nova[i-1][j] > nova[i][j-1]){
  95. i--;
  96. }
  97. else if (nova[i][j-1] > nova[i-1][j]){
  98. j--;
  99. }
  100.  
  101. }
  102.  
  103.  
  104. //for(int t=0; t<=index; t++){
  105. //printf("%s",lcs[t]);
  106. //}
  107.  
  108.  
  109.  
  110.  
  111. //lerarquivostring(arquivo1, arquivo2);
  112.  
  113. //chamar função pra ler arquivo
  114. //retonar os arquivos lidos
  115. //chamar função pra contar o tamanho das linhas
  116. //alocar dinamicamente o tamanho das linhas
  117. //colocar cada linha em uma posição do vetor
  118. //aplicar lcs
  119. //gerar a resposta
  120. //dar free
  121. //fechar o arquivo
  122. return 0;
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement