求两个字符串的最长公共子串的长度

来源:互联网 发布:nginx server name 编辑:程序博客网 时间:2024/05/13 17:26

int func(char* query, char* text)
{

if (query == NULL || text == NULL)    return 0;int len1 = strlen(query);int len2 = strlen(text);int sum = 0;int count = 0;for (int start = 0; start < len2; start++){    int i = start;    int j = 0;    count = 0;    while (i < len2 && j < len1)    {        if (query[j] == text[i])        {            i++;            j++;            count++;        }        else        {            if (sum < count)                sum = count;            i = start;            count = 0;            j++;        }    }    if (sum < count)        sum = count;}return sum;

}

int main(void)
{

char query[20];char text[100];scanf("%s",query);scanf("%s",text);int res = func(query, text);printf("%d\n",res);return 0;

}

0 0