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 nova[p+1][k+1];
- for (int i=0; i<=p; i++) {
- for (int j=0; j<=k; j++) {
- if (i == 0 || j == 0) {
- nova[i][j] = 0; }
- else if (strcmp(vetor[i-1],vetor2[j-1])== 0){
- nova[i][j] = nova[i-1][j-1] + 1;
- }
- else{
- nova[i][j] = max(nova[i-1][j], nova[i][j-1]); }
- }
- }
- /*for (i=0; i<=p; i++) {
- for (j=0; j<=k; j++) {
- printf(" %2d ", nova[i][j]);
- }
- printf("\n");
- }*/
- int index = nova[p][k];
- char lcs[index+1][0];
- //printf("%d\n",index+1);
- lcs[index][0] = '\0';
- int i = p;
- int j = k;
- //printf("Passei aqui 1.\n");
- //printf("%s", vetor[2]);
- while (i > 0 && j > 0){
- if (nova[i-1][j] == nova[i][j-1]){
- strcpy(lcs[index-1],vetor[i-1])
- i--;
- j--;
- index--;
- }
- else if (nova[i-1][j] > nova[i][j-1]){
- i--;
- }
- else if (nova[i][j-1] > nova[i-1][j]){
- j--;
- }
- }
- //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