01/* This is an alternative to the Levenstein string edit distance algorithm. 02/* By looking for similarity locally to made it really fast! 03/* Its complexity is O(n*constant) rather than O(n^2).04/* The basic idea behind it using a fast and dirty LCS (lowest common substring) length to 05/* determine the similarity between two strings. But it must ensure the size of vQuery than the size of vData or equal */06floatGetFastDistance(constvector<string>&vQuery,constvector<string>&vData)07{08intnQueryLen=vQuery.size();09intnDataLen=vData.size();1011// first consider the length factor12intnDifference=abs(nQueryLen–nTransLen);13if (nDifference>=MAXDISTANCE)14returnmax(nQueryLen,nTransLen);1516if (nQueryLen==0)17returnnDataLen ;18elseif (nDataLen==0)19returnnQueryLen;2021intc=0;22intnOffset1=0;23intnOffset2=0;24intnLCS=0;2526while ((c+nOffset1<nQueryLen) && (c+nOffset2<nDataLen))27{28if (vQuery[c+nOffset1]==vTrans[c+nOffset2])29++nLCS;30else31{32 // define FAST to make it even faster, but less reliable33#ifdef FAST34c+= (nOffset1+nOffset2) /2;35if (c>=nQueryLen)36c=nQueryLen–1;37if (c>=nDataLen)38c=nDataLen–1;39#endif40nOffset1=0;41nOffset2=0;4243if (vQuery[c]==vTrans[c])44{45++c;46continue;47}48for (inti=1; i<MAXOFFSET; ++i)49{50if ((c+i<nQueryLen) && (vQuery[c+i]==nDataLen[c]))51{52nOffset1=i;53break;54}55if ((c+i<nDataLenLen) && (vQuery[c]==nDataLen[c+i]))56{57nOffset2=i;58break;59}60}61}62++c;63}6465return ((float)nQueryLen+nTransLen) /2–nLCS;66}