2015-12-27 3 views
2

J'essaie de créer un programme qui renvoie la plus longue sous-chaîne répétée. J'ai presque la solution, mais pour une raison quelconque, mon strcmp donne une erreur lorsqu'il est occupé à trouver le LRS. Quelqu'un pourrait-il m'expliquer pourquoi il y a une erreur et comment je résous cela?C: Erreur strcmp lors de la recherche de la plus longue sous-chaîne répétée

Le code:

#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 

#define NSTRINGS 4 

char* searchLongestRepeatedSubstring(char* string); 
char** makeSuffixArray(char* string); 
void freeSuffixArray(char** suffixArray); 
int cmp(const void*a, const void* b); 

/* do not change this code */ 
int main(void) 
{ 
    char* strings[NSTRINGS] = { 
     "bananas", 
     "ask not what your country can do for you, but what you can do for your country", 
     "main", 
     "" }; 
    char* result; 

    for (int i = 0; i < NSTRINGS; ++i) 
    { 
     result = searchLongestRepeatedSubstring(strings[i]); 
     if (result != NULL) 
     { 
      /* write out LRS */ 
      printf("%s: \"%s\"\n", strings[i], result); 

      /* free() the result */ 
      free(result); 
      result = NULL; 
     } 
     else 
      printf("Geen longest repeated substring.\n"); 
    } 

    return 0; 
} 

/** 
* Finds the LRS from a string using the function makeSuffixArray 
* 
* @param the given string 
* @return the longest repeated substring 
*/ 
char* searchLongestRepeatedSubstring(char* string) 
{ 

    char **p; 
    p = makeSuffixArray(string); 
    size_t length = strlen(string); 
    int max_sz = 0; 
    char max_word; 
    int curr_sz = 0; 
    int curr_word; 
    char* word1; 
    char* word2; 
    char s1, s2; 
    size_t length1; 
    size_t length2; 

    for (size_t i = 0; i < length; i++) 
    { 
     for (size_t j = i + 1; j < length; j++) 
     { 
      word1 = p[i]; 
      word2 = p[j]; 
      length1 = strlen(word1); 
      length2 = strlen(word1); 
      for (size_t x = 0; x < length1; x++) 
      { 
       s1 = word1[x]; 
       for (size_t y = 0; y < length2; y++) 
       { 
        s2 = word2[y]; 
        if (strcmp(s1, s2) == 0) { 
         curr_sz++; 
         strcat(curr_word, s1); 
         x++; 
        } 
        else 
         break; 
       } 
      } 
      if (curr_sz > max_sz) { 
       max_sz = curr_sz; 
       curr_sz = 0; 
       max_word = curr_word; 
       curr_word = ""; 
      } 
      else { 
       curr_sz = 0; 
       curr_word = ""; 
      } 
     } 
    } 
    return max_word; 
} 

/** 
* Creates the suffix array of the given string 
* 
* @param the given string 
* @return the suffix array 
*/ 
char** makeSuffixArray(char* string) 
{ 
    size_t length = strlen(string); 
    char **p = (char**)malloc(length * sizeof(char*)); 
    for (size_t i = 0; i < strlen(string); i++) 
    { 
     p[i] = &string[i]; 
     puts(p[i]); 
    } 
    qsort(p, length, sizeof(char*), cmp); 
    return p; 
} 


int cmp(const void* a, const void* b) 
{ 
    char ** p = (char**)a; 
    char ** t = (char**)b; 

    return strcmp(*p, *t); 
} 

/** 
* free() the memory allocated for the suffix array 
* 
* @param the given suffix array 
*/ 
void freeSuffixArray(char** suffixArray) 
{ 
    free(suffixArray); 
} 
+0

Quelle erreur deviennent vous ? – Mureinik

Répondre

1

Dans votre fonction char* searchLongestRepeatedSubstring -

if (strcmp(s1, s2) == 0) { 

s1 et s2 sont les deux char variables et vous les transmettre à strcmp qui attend const char * comme arguments, donc, votre plaintes de compilateur.

Vous pouvez les comparer comme this-

if(s1==s2) 

également cette déclaration dans la même if bloc -

strcat(curr_word, s1); 

au contraire, vous pouvez le faire -

size_t len=strlen(curr_word); 
curr_word[len]=s1;     // make sure curr_word is large enough 
curr_word[len+1]='\0'; 
+0

donc j'ai besoin d'utiliser char * au lieu de char? – SpartanHero

+0

@SpartanHero Non, j'ai mentionné en réponse ce que vous devez faire :-) – ameyCU

+1

n'est-ce pas si (s1 == s2)? et merci :) – SpartanHero