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 100
- int max(int a, int b);
- /* Utility function to get max of 2 integers */
- 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 L[p+1][k+1];
- /* Following steps build L[m+1][n+1] in bottom up fashion. Note
- that L[i][j] contains length of LCS of X[0..i-1] and Y[0..j-1] */
- 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-1],vetor2[j-1])== 0)
- L[i][j] = L[i-1][j-1] + 1;
- else
- L[i][j] = max(L[i-1][j], L[i][j-1]);
- }
- }
- // Following code is used to print LCS
- int index = L[p][k];
- // Create a character array to store the lcs string
- char lcs[index+1][index+1];
- lcs[index][index] = '\0'; // Set the terminating character
- // Start from the right-most-bottom-most corner and
- // one by one store characters in lcs[]
- int i = p, j = k;
- while (i > 0 && j > 0)
- {
- // If current character in X[] and Y are same, then
- // current character is part of LCS
- if (strcmp(vetor[i-1],vetor2[j-1])==0)
- {
- strcpy(lcs[index-1],vetor[i-1]); // Put current character in result
- i--;
- j--;
- index--; // reduce values of i, j and index
- }
- // If not same, then find the larger of two and
- // go in the direction of larger value
- else if (L[i-1][j] > L[i][j-1])
- i--;
- else
- j--;
- }
- printf("%d\n", index);
- // for(int t=0; t<=index; t++){
- // printf("%s",lcs[t]);
- // }
- //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
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement