【问题标题】:Efficient Algorithm to Replace All SubArrays with Other SubArrays用其他子数组替换所有子数组的高效算法
【发布时间】:2017-05-17 00:18:37
【问题描述】:

我有一个字节数组(可以变得非常大,超过 3200 万字节),我需要用其他相同长度的子数组替换一些子数组。 我目前的方法是在字节数组中搜索我需要替换的所有子数组,每次找到一个将子数组的索引添加到列表中,然后继续。

我的代码如下。我有一种挥之不去的感觉,这效率不高,因为 3200 万字节需要超过 10 秒才能完成搜索和替换。我将 8 个字符串传递给它来替换,所以它基本上最终会搜索 16 个子数组。
有人看到我的算法有任何缺陷或更有效的吗?


附言我实际上并没有在这段代码中替换它们,只是找到索引。我的代码应该非常有效。

 public class Search
{
    public List<int> positions;
    public List<int> lengths;
    private List<byte[]> stringsToSearchFor;
    public Search(List<string> strings){
        stringsToSearchFor = new List<byte[]>();
        positions = new List<int>();
        lengths = new List<int>();
        foreach (string tempString in strings){
            stringsToSearchFor.Add(Encoding.ASCII.GetBytes(tempString));
            stringsToSearchFor.Add(Encoding.Unicode.GetBytes(tempString));
        }
    }

    public void SearchBytes(byte[] haystack){
        int[] arrayOfInt = new int[stringsToSearchFor.Count];
        bool[] arrayOfBoolean = new bool[stringsToSearchFor.Count];
        for (var i = 0; i < haystack.Length; i++){
            byte currentByte = haystack[i];
            for (int stringCounter = 0; stringCounter < arrayOfBoolean.Length; stringCounter++)
            {
                byte[] stringLookFor = stringsToSearchFor.ElementAt(stringCounter);
                byte currentStringByte = stringLookFor[arrayOfInt[stringCounter]];
                //Saying the current byte is the desired one
                if (currentStringByte == currentByte)
                {
                    if (arrayOfInt[stringCounter] + 1 == stringLookFor.Length){
                        positions.Add(i - stringLookFor.Length + 1);
                        lengths.Add(stringLookFor.Length);
                        arrayOfInt[stringCounter] = 0;
                    }
                    else
                    {
                        arrayOfInt[stringCounter]++;
                    }
                }
                else
                {
                    arrayOfInt[stringCounter] = 0;
                }
            }
        }
        return;
    }



}

【问题讨论】:

  • 什么不起作用?如果它确实有效,但你想进行审查。试试:Code review
  • 每次调用 Encoding.ASCII.GetBytes( 时都会创建一个新的字节数组。在您的情况下,最大的性能问题是内存分配。
  • 性能提升可能是检查大海捞针中stringsToSearchFor 的第一个和最后一个字符。如果currentIndex + stringsToSearchFor.Length-1 处的字符不是最后一个字符。你可以跳过当前的。
  • @JeroenvanLangen 不太清楚你的意思。 stringsToSearchFor 不仅仅是一个 byte[],它是一个 byte[] 的列表。当我调用 Encoding.ASCII.getBytes() 时我需要一个 byte[] 因为我需要搜索它。你的意思是?我不认为限制因素是内存分配,因为如果 byte[] 长度为 4096 字节,它会在大约一毫秒内运行。

标签: c# algorithm search replaceall


【解决方案1】:

您基本上是在进行蛮力搜索。您可以执行更类似于Knuth-Morris-PrattRabin-Karp 字符串搜索算法的操作,而不是蛮力(但不是在字符串中搜索字符序列,而是在数组中搜索字节序列)。

【讨论】:

    【解决方案2】:

    我可以从 SearchBytes() 只有 2 个嵌套的 for 循环这一事实中看出,这种暴力搜索算法存在错误。这种蛮力搜索需要 3 个嵌套循环:对于 haystack 中的每个起始位置,对于每个针串,您需要一个循环来检查整个针是否出现在 haystack 中的该位置。 (如果发现字符不匹配,这个最里面的循环可能会提前中止。)

    这里有一个具体的例子:如果干草堆是ABCABCABD,而你的针线之一是ABCABD,那么这个字符串将不会被找到,尽管它确实发生了。这是因为一旦你的算法在大海捞针中看到第二个C,它就会得出结论,它必须从大海捞针的当前位置开始寻找针,而实际上它需要开始从较早的位置看。

    无论如何,在长度为 n 的干草堆字符串中搜索单个长度为 m 的针字符串的蛮力时间复杂度为 O(nm),如果两者都中等长度,这是非常可怕的。 John Kurlak suggested Knuth-Morris-Pratt or Rabin-Karp,如果您正在寻找一些大字符串,运行其中任何一个肯定会加快速度(以及正确 :-P),但专门针对有效查找的算法 多个字符串中的字符串称为Aho-Corasick algorithm。它需要时间 O(n+s+k),其中 n 是大海捞针的大小,s 是要搜索的针串大小的总和,k 是任何针串的出现次数——这很漂亮很难被击败。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-09-30
      • 2016-07-04
      • 2011-11-13
      • 2017-03-04
      • 2016-04-15
      • 2020-08-25
      相关资源
      最近更新 更多