kmp算法
class StringPattern {
public:
void findnext(string B, int *next)
{
next[0] = -1;
int k = -1;
int j = 0;
while (j < B.length()-1)
{
if (k == -1 || B[j] == B[k])
{
if (B[j + 1] == B[k + 1])
{
next[++j] = next[++k];
}
else
{
next[++j] = ++k;
}
}
else
{
k = next[k];
}
}
}
int findAppearance(string A, int lena, string B, int lenb) {
int *next=new int[lenb] ;
findnext(B, next);
int i = 0,j=0;
for (i = 0; i < lena;i++)
{
while (B[j] != A[i] && j > 0)
j = next[j];
if (B[j] == A[i])
j++;
if (j == lenb)
return i - j + 1;
}
return -1;
}
};