#include<stdio.h>
#include<string.h>
char str[100];
int next[100];
void Get_next(char *str, int* next)
{
int len = strlen(str);//模式串的长度
int i = 0;
int j = 1;
next[0] = 0; //下标从0开始
while(j < len)
{
if(str[i] == str[j])
{
next[j] = next[j-1] + 1; //当匹配时
i++;
j++;
}
else
if(i == 0)
{
next[j] = 0;
i++;
j++;
}
else
i = next[i];
}
}
int Index_KMP(char *psource, char *pstr, int *next)
{
int i, j;
i = 0 ;
j = 0;
int lens = strlen(psource); //原串的长度
int lent = strlen(pstr); //模式串的长度
while(i < lens && j < lent)
{
if(psource[i] == pstr[j])//匹配时
{
i++;
j++;
}
else
if(j == 0)//当j == 0时,且不匹配时,原串应该移向下一位
i++;
else
j = next[j]; //不匹配
}
if(j >= lent)return i - lent; //匹配成功返回模式串出现的位置,注意下标是从0开始的
else return -1;
}