void GetNext( char *s , int *Next )
{
int c=0,len=strlen(s);
Next[0] = len;
while( s[c]==s[c+1]&&c+1<len ) c++;
Next[1] = c;
int po = 1;
for ( int i=2 ; i<len ; i++ )
{
if ( Next[i-po]+i<Next[po]+po )
Next[i] = Next[i-po];
else
{
int t = Next[po]+po-i;
if ( t<0 ) t = 0;
while( i+t<len&&s[t]==s[i+t] ) t++;
Next[i] = t;
po = i;
}
}
}
void ExKmp( char *s1 , char *s2 , int *Next , int *Ex )
{
int c=0,len=strlen(s1),l2=strlen(s2);
GetNext( s2 , Next );
while( s1[c]==s2[c]&&c<len&&c<l2 ) c++;
Ex[0] = c;
int po = 0;
for ( int i=1 ; i<len ; i++ )
{
if ( Next[i-po]+i<Ex[po]+po )
Ex[i] = Next[i-po];
else
{
int t = Ex[po]+po-i;
if ( t<0 ) t = 0;
while( i+t<len&&t<l2&&s1[i+t]==s2[t] ) t++;
Ex[i] = t;
po = i;
}
}
}
扩展KMP模板
猜你喜欢
转载自blog.csdn.net/game_acm/article/details/80998361
今日推荐
周排行