其实无论是你这里的+N还是taocp里面求逆序中的改用相反数,本质都相同,都是使用了数据中一些没有被使用的bit位。
而对于这道题目,在n <2^31时,如果可以这样做,还有更加简单的方法。
直接将原数组的最高比特位看作一个比特位数组就可以了
- C/C++ code
-
bool duplicate=false;
for(i=0;i<N;i++){
int x=abs(a[i]);
if(a[x-1]<0){
duplicate=true;
break;
}else{
a[x-1]=-a[x-1];
}
}
for(i=0;i<N;i++){
a[i]=abs(a[i]);
}
return duplicate;
…
]]>
Abstract Given two strings S of length m and string T of length n, the paper presents a new algorithm for calculating the similarity of the two strings. By the LCSubstr (longest common substring) algorithm we can find the maximal matching of the two given strings. Then eliminate the LCSubstr we will get two temp result strings. My algorithm will calculate the temp result strings iteratively until the two result strings’ common string is null. The similarity of the two strings will be measured by accumulating the non-linear mapping length of the maximal matched substring. The algorithm is always searching for the maximal continuous matching (MCM) in every step. In the end of the article I will introduce an application of this algorithm.
Keywords: pattern matching, LCSubstr, non-linear mapping, string similarity, maximal continuous matching (MCM)