如何找到指定序列的"ordinal hash code"?

How to find the "ordinal hash code" of specified sequence?

我有一个序列[-1,-2,-3,-4,0,1,2,3],任务是达到目标序列[1,2,3,0,-1,-2,-3,-4]。符号 "0" 表示间隙 - 它可以换成非间隙符号,如下所示: [-1,-2,-3,-4,1,0,2,3]。在初始序列的每次操作中,成本为 0(零)。我需要知道每个连续序列与目标序列有多接近的系数。它可能是 [0.0 - 1.0] 范围内的值,其中 1.0 表示目标序列,0.0 表示初始序列,或者它可以是哈希- 类型 INTEGER 的代码值,但随着目标序列的每次接近,该值应该越来越小。任何想法如何实施?我尝试了字符串比较算法(编辑距离),但它们不适用于这种情况。

假设所有数字都是唯一的。

前方未经测试的代码

using SumType = int64_t; // feel free to shoot yourself in the foot by using floats.
SumType PosSum(const std::vector<int>& curr, const std::vector<int>& goal) {
  SumType sum = 0;
  for(auto it = curr.begin(); it != curr.end(); ++it) {
    // if (*it != gabValue) ???
    sum += std::abs(std::distance(it, std::find(goal.begin(), goal.end(), *it));
  }
}

或者使用 std::unordered_map 来存储目标位置,可能会更快。

auto init = PosSum(start, goal);
if (init == 0)
  return;

...whatever

auto now = PosSum(current, goal); // slight problem as init is not guaranteed to be a global minima.

auto coeff = double(1.0) - double(now)/double(init); // which makes this [x:1] where x can be zero or negative