Welcome, Guest! Sign Up RSS

Clever Space

Friday, 11.22.2024
Main » 2013 » October » 4 » C++ KMP模板
6:26 AM
C++ KMP模板

C++:

void KMP()
{
  //Init
  int i,j,old_i;
  P[1]=0,j=0;
  FOR(i,2,m)
  {
    while ((j>0)&&(B[j+1]!=B[i])) j=P[j];
    if (B[j+1]==B[i]) j++;
    P[i]=j;
  }
  //Doit
  j=0;
  FOR(i,1,n)
  {
    while ((j>0)&&(B[j+1]!=A[i])) j=P[j];
    if (B[j+1]==A[i]) j++;
    if (j==m){ans++;j=P[j];}
  }
}
Views: 347 | Added by: dhy0077 | Rating: 5.0/1
Total comments: 0
Only registered users can add comments.
[ Sign Up | Login ]