Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在由数字组成的字符串中查找未知重复模式_C#_String_Pattern Matching_Repeat - Fatal编程技术网

C# 在由数字组成的字符串中查找未知重复模式

C# 在由数字组成的字符串中查找未知重复模式,c#,string,pattern-matching,repeat,C#,String,Pattern Matching,Repeat,我已经为此挣扎了一个星期 我有一个这样的字符串:1011010110110110101011011010101011010110110110101….. 我需要找到的内容:11 10 例二:111200220021101201201202202101200220022002101012012002021010…. 我需要找到的内容:1 12 0 2 1 0 例3:1112310112310112310112310112310112310112310110… 112310 等等等等 我现在的

我已经为此挣扎了一个星期

我有一个这样的字符串:1011010110110110101011011010101011010110110110101…..

我需要找到的内容:11 10


例二:111200220021101201201202202101200220022002101012012002021010….

我需要找到的内容:1 12 0 2 1 0


例3:1112310112310112310112310112310112310112310110…

112310


等等等等

我现在的代码是:

private string tekrarArama(double[] mods)
        {
            string part1 = "";
            string part2 = "";

            string patern = "";


            int number1 = mods.Length;

            for (int i = 0; i < mods.Length; i++)
            {
                part1 = "";
                part2 = "";
                for (int j = 0; j < number1; j++)
                {

                    part1 += mods[j] + ",";
                }


                for (int k = 0; k < mods.Length; k++)
                {

                    part2 += mods[k] + ",";
                }


                int actualcount = Regex.Matches(part2, part1).Count;

                int count = part2.Replace(",", "").Length / part1.Replace(",", "").Length;


                if (part2.IndexOf(bolum1) >= 0 && actualcount == count )
                {
                    patern = part2.Substring(part2.IndexOf(part1),part1.Length);
                }

                number1--;


            }

            return patern;

        }
私有字符串Tekrarama(双[]mods)
{
字符串part1=“”;
字符串部分2=“”;
字符串patern=“”;
int number1=模块长度;
对于(int i=0;i=0&&actualcount==count)
{
patern=第2部分子字符串(第2部分索引(第1部分),第1部分长度);
}
1号--;
}
返回模式;
}
它创建字符串的两个副本,并在每次迭代中从其中一个字符串中删除一个字符,以找到最小的重复模式

它一团糟,一点也不好用。
我该怎么做?提前感谢。

如果您想要的是简单的东西,而不需要最佳的复杂性,这里有一种简洁的方法来表示查询

string FindPattern(string text)
{
    if (text == null)
    {
        return null;
    }

    return Enumerable
        .Range(1, text.Length / 2)
        .Where(n => text.Length % n == 0)
        .Select(n => text.Substring(0, n))
        .Where(pattern => Enumerable
            .Range(0, text.Length / pattern.Length)
            .SelectMany(i => pattern)
            .SequenceEqual(text))
        .FirstOrDefault();
}

请注意,在最坏的情况下,这里的复杂度是二次的,因此在很长的字符串中使用它不是一个好主意。

是吗?@PatrykĆwiek我认为他不是在寻找最长的字符串。在给出的第一个示例中,110不是最长的重复子字符串。如果先将算法描述为伪代码,然后用铅笔和纸在桌面上运行,您可能会更幸运。@PatrykĆwiek更像是,最短字符串:)谢谢!这正是我需要的。