Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #define MAX 300
- int max(int a, int b);
- int max(int a, int b)
- {
- if (a>b){
- return a;
- }
- else {
- return b;
- }
- }
- int main(int argc, char* argv[ ]){
- FILE *arq1;
- FILE *arq2;
- arq1 = fopen(argv[1],"r");
- arq2 = fopen(argv[2],"r");
- if (arq1 == NULL){
- printf("!!!!ERROR!!!!\n");
- exit;
- }
- if (arq2 == NULL){
- printf("!!ERROR!!\n");
- exit;
- }
- char palavra[MAX];
- char vetor[MAX][MAX];
- char palavra2[MAX];
- char vetor2[MAX][MAX];
- int p = 0,k = 0;
- setbuf(stdin, NULL);
- while (fgets(palavra, MAX, arq1)){
- strcpy(vetor[p], palavra);
- p++;
- }
- while (fgets(palavra2, MAX, arq2)){
- strcpy(vetor2[k], palavra2);
- k++;
- }
- fclose(arq1);
- fclose(arq2);
- //printf("%d %d\n", p, k);
- int **aloc_int;
- aloc_int = (int**)calloc(p,sizeof(int*));
- for(int h = 0; h<p; h++){
- aloc_int[h] = (int*)calloc(k,sizeof(int));
- }
- if (aloc_int == NULL){
- printf("Houve um erro\n");
- }
- printf("Sem erros no aloc\n");
- int L[p][k];
- for (int i=0; i<=p; i++) {
- for (int j=0; j<=k; j++) {
- if (i == 0 || j == 0) {
- L[i][j] = 0; }
- else if (strcmp(vetor[i],vetor2[j])== 0){
- L[i][j] = L[i-1][j-1] + 1;
- }
- else{
- L[i][j] = max(L[i-1][j], L[i][j-1]); }
- }
- }
- // for(int q = 0; q < p+1; q++){
- // for (int r=0; r < k+1; r++){
- // printf(" %2d ",L[q][r]);
- // }
- // printf("\n");
- // }
- int index = L[p][k];
- printf("O index é %d\n",index);
- char lcs[index][index];
- int i = p;
- int j = k;
- while (i > 0 && j > 0){
- if (L[i-1] == L[j-1]){
- // printf("So testando %d\n",i);
- strcpy(lcs[index],vetor[i]);
- printf("%s",lcs[index]);
- i--;
- j--;
- index--;
- }
- else if (L[i-1][j] > L[i][j-1]){
- i--;
- }
- else{
- j--;
- }
- }
- //printf("%s",lcs[index]);
- for(int h =0; h<p; h++){
- free(aloc_int[h]);
- }
- free(aloc_int);
- //lerarquivostring(arquivo1, arquivo2);
- //chamar função pra ler arquivo
- //retonar os arquivos lidos
- //chamar função pra contar o tamanho das linhas
- //alocar dinamicamente o tamanho das linhas
- //colocar cada linha em uma posição do vetor
- //aplicar lcs
- //gerar a resposta
- //dar free
- //fechar o arquivo
- printf("Fim do programa.\n");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement