| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
终于理解了KMP, 发个改编算法导论的KMP纪念!void Kmp(char *t, char *pat)
{
pref[0] = -1;
int k = -1, i;
for(i = 1; pat[i] != 0; ++i)
{
while(k > -1 && pat[k+1] != pat[i])
k = pref[k];
if(pat[k+1] == pat[i])
++k;
pref[i] = k;
}
k = -1;
for(i = 0; t[i] != 0; ++i)
{
while(k > -1 && pat[k+1] != t[i])
k = pref[k];
if(pat[k+1] == t[i])
++k;
if(pat[k+1] == 0)//k==strlen(pat)-1
{
//printf("matched at %d\n", i-k);
++cnt;
k = pref[k];
}
}
}
Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator