0

假设我有两个字节数组,每个数组都包含一系列值,例如:

byte[] b = {50,60,70,80,90,10,20,1,2,3,4,5,50,2,3,1,2,3,4,5};
byte[] b2 = {1,2,3,4,5}

我可以比较这两个数组并使用 LinQ 方法查找相等的值。这样,如果我在这两个数组之间进行比较,结果将是 b 数组的索引,其中 b2 数组的索引中的值是匹配的。

我一直在尝试准确找到 b2 数组在 b 数组中重复出现的范围。我是说

if (TheLenghtOfSearch==5) {Now the indexes of two regions must be return }
Result ->(7, 11), (15, 19)

if (TheLenghtOfSearch==2) {Now the indexes of around 9 regions where the two consecutive values in b2 recurred in b must be returned}
Result ->(7, 8), (15, 16), (8, 9), (13, 14), (16, 17), (9, 10), (17, 18), (10, 11), (18, 19)

我想解决方案更数学。

4

2 回答 2

0

我决定使用列表,而不是数组,因为它有更多此类操作的助手。据我了解深度=每个数组中必须相等的项目数量。这个有效,看看这个:

class Program
{
    static void Main(string[] args)
    {
        List<byte> b = new List<byte>() { 50, 60, 70, 80, 90, 10, 20, 1, 2, 3, 4, 5, 50, 2, 3, 1, 2, 3, 4, 5 };
        List<byte> b2 = new List<byte>() { 1, 2, 3, 4, 5 };
        SmartComparer comparer = new SmartComparer();
        //Setting the depth here, now the depth is = 5
        var result = comparer.CompareArraysWithDepth(b, b2, 5);
        foreach (var keyValuePair in result)
        {
            Console.WriteLine(String.Format("b[{0}]->b[{1}] are equal to b2[{2}]->b2[{3}]", keyValuePair.Key.Key,
                                            keyValuePair.Key.Value, keyValuePair.Value.Key, keyValuePair.Value.Value));
        }
    }
}

public class SmartComparer
{
    public Boolean CompareRange(List<byte> a, List<byte> b)
    {
        for (int i = 0; i < a.Count; i++)
        {
            if (a[i] != b[i])
            {
                return false;
            }
        }
        return true;
    }

    /// <summary>
    /// |
    /// </summary>
    /// <param name="a"></param>
    /// <param name="b"></param>
    /// <param name="depth"></param>
    /// <returns>Key->range in 'a', Value->range in 'b'</returns>
    public List<KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>>> CompareArraysWithDepth(
        List<byte> a, List<byte> b, int depth)
    {
        var result = new List<KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>>>();
        if (depth > b.Count)
            throw new ArgumentException("Array 'b' item count should be more then depth");
        if(a.Count<b.Count)
            throw new ArgumentException("Array 'a' item count should be more then Array 'b' item count");
        for (int i = 0; i <= a.Count - depth; i++)
        {
            for (int j = 0; j <= b.Count - depth; j++)
            {
                if (CompareRange(a.GetRange(i, depth), b.GetRange(j, depth)))
                {
                    result.Add(new KeyValuePair<KeyValuePair<int, int>, KeyValuePair<int, int>>(new KeyValuePair<int, int>(i, i + depth-1), new KeyValuePair<int, int>(j, j + depth-1)));
                }
            }
        }
        return result;
    }
}

添加

对于深度 = 3,此操作的结果:

b[7]->b[9] are equal to b2[0]->b2[2]
b[8]->b[10] are equal to b2[1]->b2[3]
b[9]->b[11] are equal to b2[2]->b2[4]
b[15]->b[17] are equal to b2[0]->b2[2]
b[16]->b[18] are equal to b2[1]->b2[3]
b[17]->b[19] are equal to b2[2]->b2[4]

对于深度 = 2,此操作的结果:

b[7]->b[8] are equal to b2[0]->b2[1]
b[8]->b[9] are equal to b2[1]->b2[2]
b[9]->b[10] are equal to b2[2]->b2[3]
b[10]->b[11] are equal to b2[3]->b2[4]
b[13]->b[14] are equal to b2[1]->b2[2]
b[15]->b[16] are equal to b2[0]->b2[1]
b[16]->b[17] are equal to b2[1]->b2[2]
b[17]->b[18] are equal to b2[2]->b2[3]
b[18]->b[19] are equal to b2[3]->b2[4]

对于深度 = 5,此操作的结果:

b[7]->b[11] are equal to b2[0]->b2[4]
b[15]->b[19] are equal to b2[0]->b2[4]
于 2013-07-23T07:56:22.393 回答
0

如果 Linq 不是您绝对需要的选项,您可以通过 for 循环获得结果:

public static IList<Tuple<int, int>> RecurringIndexes(Byte[] master, Byte[] toFind, int length) {
  List<Tuple<int, int>> result = new List<Tuple<int, int>>();

  // Let's return empty list ... Or throw appropriate exception
  if (Object.ReferenceEquals(null, master))
    return result;
  else if (Object.ReferenceEquals(null, toFind))
    return result;
  else if (length < 0)
    return result;
  else if (length > toFind.Length)
    return result;

  Byte[] subRegion = new Byte[length];

  for (int i = 0; i <= toFind.Length - length; ++i) {
    for (int j = 0; j < length; ++j)
      subRegion[j] = toFind[j + i];

    for (int j = 0; j < master.Length - length + 1; ++j) {
      Boolean counterExample = false;

      for (int k = 0; k < length; ++k)
        if (master[j + k] != subRegion[k]) {
          counterExample = true;

          break;
        }

      if (counterExample)
        continue;

      result.Add(new Tuple<int, int>(j, j + length - 1));
    }
  }

  return result;
}

....

byte[] b = {50,60,70,80,90,10,20,1,2,3,4,5,50,2,3,1,2,3,4,5};
byte[] b2 = { 1, 2, 3, 4, 5 };

// Returns 2 patterns: {(7, 11), (15, 19)}
IList<Tuple<int, int>> indice5 = RecurringIndexes(b, b2, 5); 
// Return 9 patterns: {(7, 8), (15, 16), (8, 9), (13, 14), (16, 17), (9, 10), (17, 18), (10, 11), (18, 19)}
IList<Tuple<int, int>> indice2 = RecurringIndexes(b, b2, 2);
于 2013-07-23T07:26:51.370 回答