int KMP(const char *original, int o_len, const char *substring, int s_len){
if(o_len < s_len)
return -1;
int k = 0;
int cur = 1;
int fail[ s_len ];
fail[ k ] = -1;
while(cur < s_len){
k = cur - 1;
do{
if(substring[ cur ] == substring[ k ]){
fail[ cur ] = k;
break;
}else{
k = fail[ k ] + 1;
}
}while(k);
if(!k && (substring[ cur ] != substring[ 0 ])){
fail[ cur ] = -1;
}else if(!k){
fail[ cur ] = 0;
}
cur++;
}
k = 0;
cur = 0;
while((k < s_len) && (cur < o_len)){
if(original[ cur ] == substring[ k ]){
cur++;
k++;
}else{
if(k == 0){
cur++;
}else{
k = fail[ k - 1 ] + 1;
}
}
}
if(k == s_len)
return cur - k;
else
return -1;
}
這是我曾經編碼過的KMP算法。當我今天上午審查它時,我發現奇怪的是整數數組被定義爲int fail [s_len]。規範是否需要數組的編譯時常量?這段代碼如何通過編譯? 順便說一句,我的gcc版本是4.4.1。 在此先感謝!以C語言約束陣列維數