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];} } }
|
|
Total comments: 0 | |