在较长的序列中查找子序列

本文关键字:查找 | 更新日期: 2023-09-27 18:07:38

我需要在其他大序列中找到一个序列,例如,{1,3,2,3}存在于{1,3,2,3,4,3}{5,1,3,2,3}中。有没有办法用IEnumerable或其他东西快速地做到这一点?

在较长的序列中查找子序列

类似于@dlev的,但这也处理{1,1,1,2}.ContainsSubsequence({1,1,2})

public static bool ContainsSubsequence<T>(this IEnumerable<T> parent, IEnumerable<T> target)
{
    var pattern = target.ToArray();
    var source = new LinkedList<T>();
    foreach (var element in parent) 
    {
        source.AddLast(element);
        if(source.Count == pattern.Length)
        {
            if(source.SequenceEqual(pattern))
                return true;
            source.RemoveFirst();
        }
    }
    return false;
}

此方法将在父序列中找到可通过Equals()进行比较的任何类型的子序列:

public static bool ContainsSubequence<T>(this IEnumerable<T> parent, IEnumerable<T> target)
{
    bool foundOneMatch = false;
    using (IEnumerator<T> parentEnum = parent.GetEnumerator())
    {
        using (IEnumerator<T> targetEnum = target.GetEnumerator())
        {
            // Get the first target instance; empty sequences are trivially contained
            if (!targetEnum.MoveNext())
                return true;
            while (parentEnum.MoveNext())
            {
                if (targetEnum.Current.Equals(parentEnum.Current))
                {
                    // Match, so move the target enum forward
                    foundOneMatch = true;
                    if (!targetEnum.MoveNext())
                    {
                        // We went through the entire target, so we have a match
                        return true;
                    }
                }
                else if (foundOneMatch)
                {
                    return false;
                }
            }
            return false;
        }
    }
}

你可以这样使用:

bool match = new[] {1, 2, 3}.ContainsSubsequence(new[] {1, 2}); // match == true
match = new[] {1, 2, 3}.ContainsSubsequence(new[] {1, 3}); // match == false

注意,它假设目标序列没有null元素。

更新:感谢大家的支持,但实际上在上面的代码中有一个错误 !如果找到了部分匹配,但没有转换为完全匹配,则该过程将以结束,而不是重置(当应用于{1, 2, 1, 2, 3}.ContainsSubsequence({1, 2, 3})时,这显然是不正确的)。

上面的代码对于更常见的子序列定义(即不需要连续性)来说工作得很好,但是为了处理重置(大多数IEnumerators不支持),需要先枚举目标序列。这导致了以下代码:

public static bool ContainsSubequence<T>(this IEnumerable<T> parent, IEnumerable<T> target)
{
    bool foundOneMatch = false;
    var enumeratedTarget = target.ToList();
    int enumPos = 0;
    using (IEnumerator<T> parentEnum = parent.GetEnumerator())
    {
        while (parentEnum.MoveNext())
        {
            if (enumeratedTarget[enumPos].Equals(parentEnum.Current))
            {
                // Match, so move the target enum forward
                foundOneMatch = true;
                if (enumPos == enumeratedTarget.Count - 1)
                {
                    // We went through the entire target, so we have a match
                    return true;
                }
                enumPos++;
            }
            else if (foundOneMatch)
            {
                foundOneMatch = false;
                enumPos = 0;
                if (enumeratedTarget[enumPos].Equals(parentEnum.Current))
                {
                    foundOneMatch = true;
                    enumPos++;
                }
            }
        }
        return false;
    }
}

此代码没有任何错误,但对于大(或无限)序列不能很好地工作。

这是一个很好的研究问题,根据我的研究,有两种算法是最适合这项工作的,这取决于你的数据。

Knuth-Morris- p ratt算法和Boyer-Moore算法。

在这里,我提交了我的KMP算法的实现,最初在这里审查。

用来处理长度不超过Int64.MaxValue的源序列或父序列。

可以看到,内部实现返回子字符串或目标模式所在的索引序列。您可以通过选择外观来呈现这些结果。

你可以像这样简单地使用

var contains = new[] { 1, 3, 2, 3, 4, 3 }.Contains(new[] { 1, 3, 2, 3 });

下面是一个显示实际代码的工作小提琴。

下面是我的答案的完整注释代码。

namespace Code
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    /// <summary>
    /// A generic implementation of the Knuth-Morris-Pratt algorithm that searches,
    /// in a memory efficient way, over a given <see cref="IEnumerable{T}"/>.
    /// </summary>
    public static class KMP
    {
        /// <summary>
        /// Determines whether a sequence contains the search string.
        /// </summary>
        /// <typeparam name="T">
        /// The type of elements of <paramref name="source"/>
        /// </typeparam>
        /// <param name="source">
        /// A sequence of elements
        /// </param>
        /// <param name="pattern">The search string.</param>
        /// <param name="equalityComparer">
        /// Determines whether the sequence contains a specified element.
        /// If <c>null</c>
        /// <see cref="EqualityComparer{T}.Default"/> will be used.
        /// </param>
        /// <returns>
        /// <c>true</c> if the source contains the specified pattern;
        /// otherwise, <c>false</c>.
        /// </returns>
        /// <exception cref="ArgumentNullException">pattern</exception>
        public static bool Contains<T>(
                this IEnumerable<T> source,
                IEnumerable<T> pattern,
                IEqualityComparer<T> equalityComparer = null)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }
            equalityComparer = equalityComparer ?? EqualityComparer<T>.Default;
            return SearchImplementation(source, pattern, equalityComparer).Any();
        }
        public static IEnumerable<long> IndicesOf<T>(
                this IEnumerable<T> source,
                IEnumerable<T> pattern,
                IEqualityComparer<T> equalityComparer = null)
        {
            if (pattern == null)
            {
                throw new ArgumentNullException(nameof(pattern));
            }
            equalityComparer = equalityComparer ?? EqualityComparer<T>.Default;
            return SearchImplementation(source, pattern, equalityComparer);
        }
        /// <summary>
        /// Identifies indices of a pattern string in a given sequence.
        /// </summary>
        /// <typeparam name="T">
        /// The type of elements of <paramref name="source"/>
        /// </typeparam>
        /// <param name="source">
        /// The sequence to search.
        /// </param>
        /// <param name="patternString">
        /// The string to find in the sequence.
        /// </param>
        /// <param name="equalityComparer">
        /// Determines whether the sequence contains a specified element.
        /// </param>
        /// <returns>
        /// A sequence of indices where the pattern can be found
        /// in the source.
        /// </returns>
        /// <exception cref="ArgumentOutOfRangeException">
        /// patternSequence - The pattern must contain 1 or more elements.
        /// </exception>
        private static IEnumerable<long> SearchImplementation<T>(
            IEnumerable<T> source,
            IEnumerable<T> patternString,
            IEqualityComparer<T> equalityComparer)
        {
            // Pre-process the pattern
            (var slide, var pattern) = GetSlide(patternString, equalityComparer);
            var patternLength = pattern.Count;
            if (patternLength == 0)
            {
                throw new ArgumentOutOfRangeException(
                    nameof(patternString),
                    "The pattern must contain 1 or more elements.");
            }
            var buffer = new Dictionary<long, T>(patternLength);
            var more = true;
            long sourceIndex = 0; // index for source
            int patternIndex = 0; // index for pattern
            using(var sourceEnumerator = source.GetEnumerator())
            while (more)
            {
                more = FillBuffer(
                        buffer,
                        sourceEnumerator,
                        sourceIndex,
                        patternLength,
                        out T t);
                if (equalityComparer.Equals(pattern[patternIndex], t))
                {
                    patternIndex++;
                    sourceIndex++;
                    more = FillBuffer(
                        buffer,
                        sourceEnumerator,
                        sourceIndex,
                        patternLength,
                        out t);
                }
                if (patternIndex == patternLength)
                {
                    yield return sourceIndex - patternIndex;
                    patternIndex = slide[patternIndex - 1];
                }
                else if (more && !equalityComparer.Equals(pattern[patternIndex], t))
                {
                    if (patternIndex != 0)
                    {
                        patternIndex = slide[patternIndex - 1];
                    }
                    else
                    {
                        sourceIndex = sourceIndex + 1;
                    }
                }
            }
        }
        /// <summary>
        /// Services the buffer and retrieves the value.
        /// </summary>
        /// <remarks>
        /// The buffer is used so that it is not necessary to hold the
        /// entire source in memory.
        /// </remarks>
        /// <typeparam name="T">
        /// The type of elements of <paramref name="source"/>.
        /// </typeparam>
        /// <param name="buffer">The buffer.</param>
        /// <param name="source">The source enumerator.</param>
        /// <param name="sourceIndex">The element index to retrieve.</param>
        /// <param name="patternLength">Length of the search string.</param>
        /// <param name="value">The element value retrieved from the source.</param>
        /// <returns>
        /// <c>true</c> if there is potentially more data to process;
        /// otherwise <c>false</c>.
        /// </returns>
        private static bool FillBuffer<T>(
            IDictionary<long, T> buffer,
            IEnumerator<T> source,
            long sourceIndex,
            int patternLength,
            out T value)
        {
            bool more = true;
            if (!buffer.TryGetValue(sourceIndex, out value))
            {
                more = source.MoveNext();
                if (more)
                {
                    value = source.Current;
                    buffer.Remove(sourceIndex - patternLength);
                    buffer.Add(sourceIndex, value);
                }
            }
            return more;
        }
        /// <summary>
        /// Gets the offset array which acts as a slide rule for the KMP algorithm.
        /// </summary>
        /// <typeparam name="T">
        /// The type of elements of <paramref name="source"/>.
        /// </typeparam>
        /// <param name="pattern">The search string.</param>
        /// <param name="equalityComparer">
        /// Determines whether the sequence contains a specified element.
        /// If <c>null</c>
        /// <see cref="EqualityComparer{T}.Default"/> will be used.
        /// </param>
        /// <returns>A tuple of the offsets and the enumerated pattern.</returns>
        private static (IReadOnlyList<int> Slide, IReadOnlyList<T> Pattern) GetSlide<T>(
                IEnumerable<T> pattern,
                IEqualityComparer<T> equalityComparer)
        {
            var patternList = pattern.ToList();
            var slide = new int[patternList.Count];
            int length = 0;
            int patternIndex = 1;
            while (patternIndex < patternList.Count)
            {
                if (equalityComparer.Equals(
                        patternList[patternIndex],
                        patternList[length]))
                {
                    length++;
                    slide[patternIndex] = length;
                    patternIndex++;
                }
                else
                {
                    if (length != 0)
                    {
                        length = slide[length - 1];
                    }
                    else
                    {
                        slide[patternIndex] = length;
                        patternIndex++;
                    }
                }
            }
            return (slide, patternList);
        }
    }
}

您可以尝试这样的操作来开始。将此列表转换为字符串后,可以使用子字符串

查找序列:
if (String.Join(",", numericList.ConvertAll<string>(x => x.ToString()).ToArray())
{
    //get sequence
}

此函数使用LINQ检查列表parent是否包含列表target:

    public static bool ContainsSequence<T>(this List<T> parent, List<T> target)
    {
        for (int fromElement = parent.IndexOf(target.First());
            (fromElement != -1) && (fromElement <= parent.Count - target.Count);
            fromElement = parent.FindIndex(fromElement + 1, p => p.Equals(target.First())))
        {
            var comparedSequence = parent.Skip(fromElement).Take(target.Count);
            if (comparedSequence.SequenceEqual(target)) return true;
        }
        return false;
    }       

如果您正在处理简单的可序列化类型,您可以很容易地做到这一点,如果您将数组转换为字符串:

public static bool ContainsList<T>(this List<T> containingList, List<T> containedList)
{
    string strContaining = "," + string.Join(",", containingList) + ",";
    string strContained = "," + string.Join(",", containedList) + ",";
    return strContaining.Contains(strContained);
}
注意这是一个扩展方法,所以你可以这样调用它:
if (bigList.ContainsList(smallList))
{
    ...
}

这对我有用

var a1 = new List<int> { 1, 2, 3, 4, 5 };
var a2 = new List<int> { 2, 3, 4 };
int index = -1;
bool res = a2.All(
    x => index != -1 ? (++index == a1.IndexOf(x)) : ((index = a1.IndexOf(x)) != -1)
);

这里有一个日常工作的示例,到目前为止还没有问题。我只是迭代一个相当长的序列,寻找匹配的给定子序列。

static bool SubSequenceExist(byte[] longSeq, byte[] subseq)
    {
        int seq = longSeq.Length;
        int seqToFindLen = subseq.Length;
        int seqMatchCount = 0;
        for (int i = 0; i < seq; i++)
        {
            if (longSeq[i] == subseq[seqMatchCount])
                seqMatchCount++;
            else
                seqMatchCount = 0;
            if (seqMatchCount == seqToFindLen) return true;
        }
        return false;
    }