KMP程序出现异常,求各位大神帮忙解决一下,谢谢
运行程序出现异常,调试的时候说异常出现在红色行,代码如下:
#include <iostream>
#include <string.h>
using namespace std;
#define MAX_STRING_SIZE 1024
class CmyString
{
public:
CmyString(char *s);
~CmyString();
//int Find(CmyString *CS,CmyString *s);
int getLength();
char* getString();
void operator=(CmyString *s);
private:
int length;
char *str[MAX_STRING_SIZE + 1];
};
CmyString::CmyString(char *s)
{
strcpy_s(*str,strlen(s),s);
length = strlen(s);
}
CmyString ::~CmyString()
{
}
int CmyString::getLength()
{
return length;
}
char* CmyString::getString()
{
return str[MAX_STRING_SIZE + 1];
}
void CmyString::operator=(CmyString *s)
{
strcpy_s(*str,strlen(s->getString()), s->getString());
length = s->getLength();
}
void GenKMPNext(int *next, CmyString *s)
{
int i = 0, j = -1;
next[0] = -1;
char *str = s->getString();
while (i < (s->getLength()- 1))
{
while (j >= 0 && str[i] != str[j])
j = next[j];
i++;
j++;
if (str[i] == str[j])
next[i] = next[j];
else next[i] = j;
}
}
int Find(CmyString *CS, CmyString *s)
{
int i, j;
CmyString *ss;
ss = s;
char *str1 = ss->getString();
char *str2 = CS->getString();
int *next=(int *)malloc(sizeof(int)*s->getLength());
GenKMPNext(next,ss);
for (i = 0, j = 0; i < ss->getLength() && j < CS->getLength();)
{
if (str1[i] == str2[j])
{
i++; j++;
}
else if (next[i] >= 0)
i = next[i];
else
{
i = 0; j++;
}
}
if (i >= ss->getLength())
return j - ss->getLength();
else
return -1;
}
int main()
{
char p[MAX_STRING_SIZE];
char target[MAX_STRING_SIZE];
cin >> p;
//char *m = (char *)p;
cin >> target;
CmyString s(p);
CmyString cs(target);
int result;
result = Find(&cs,&s);
if (result == -1)
cout << "False" << endl;
else
cout << "True" << endl;
return 0;
}
我使用的是VS2015。请各位大神帮忙看一下。目的是要实现KMP算法