我有兩個二進制數組,一個大小爲34(模式),另一個大小爲10000(目標)。 我想看看是否有任何模式在目標與閾值(如最多4個不匹配) 和返回匹配數(沒有重疊發生,如果一個匹配,然後下一個匹配將遠離800個單元格)。 我知道這是一種近似匹配問題,但我不知道使用哪種算法具有最佳性能。我做了什麼至今:(方法LIKE2具有更好的性能)比較兩個二進制數組與閾值(近似匹配)
void compare (bool *target, int t, bool * pattern , int p , int threshold)
{
for(int i =0;i<t-p;i++){
if(like(target+i,pattern,p,threshold)){
return true;
}
}
return false;
}
void like2(bool *target, bool * pattern , int p , int threshold){
int k =0;
for(int i =0;i<p, ;i++){
k+= target[i]^pattern [i];
}
return (k<=threshold);
}
void like(bool *target, bool * pattern , int p , int threshold){
int k =threshold;
for(int i =0;i<p,k>=0 ;i++){
if(target[i]!=pattern[i]){
--k;
}
}
return (k >=0);
}
我試過用字符串匹配算法,如克努特莫里斯普拉特算法,但它們是精確匹配,並改變它們近似匹配算法是一個很難的方法。
當'k'已經超過閾值時加入提前返回 – timrau
它不會提高性能! – abdolahS
@abdolahS你是什麼意思,如果有一場(接近)比賽,那麼下一場比賽至少會有800「小格」?你是否貪婪地從左到右搜索大串中的(接近)比賽,然後如果你找到一個,那麼你在大串中向右移動800個位置來尋找下一個接近的匹配? – user2566092