C:找到最长的重复子字符串时出现strcmp错误

时间:2015-12-27 16:13:20

标签: c strcmp suffix-array

我正在尝试创建一个返回最长重复子字符串的程序。我几乎得到了解决方案,但由于某种原因,我的strcmp在他忙着找到LRS时会出错。有人可以解释一下为什么会出现错误以及我如何解决这个问题?

代码:

#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);
}

1 个答案:

答案 0 :(得分:1)

在您的函数 Bundle bundle = new Bundle(); bundle.putString("fields","first_name,last_name"); new GraphRequest( accessToken,"/me",bundle, -

char* searchLongestRepeatedSubstring

if (strcmp(s1, s2) == 0) { s1都是s2变量,您将它们传递给charstrcmp作为参数,因此,您的编译器投诉

您可以像这样比较它们 -

const char *

此语句也在同一if(s1==s2) 块中 -

if

相反,你可以这样做 -

strcat(curr_word, s1);