0001 int match(char* T, int n, char* P, int m) { //KMP主算法 0002 int i = 0, j = 0; //当前字符位置 0003 int* next = new int[m]; buildNext(P, m, next); //构造next表 0004 while ((j < m) && (i-j <= n-m)) //自左向右逐个比对字符 0005 if (0 > j || T[i] == P[j]) //若匹配(包括与假想通配符的匹配) 0006 { i++; j++; } //则转到下一对字符 0007 else //否则 0008 j = next[j]; //查表确定P[j]的替代者(T[i]绝不回退) 0009 delete[] next; //释放next表 0010 return i-j; //最后一次对齐的位置,足以判断是否匹配、在何处匹配 0011 } //match