Algorithm 找出重复2次或以上的最长子串

Algorithm 找出重复2次或以上的最长子串,algorithm,Algorithm,我只想编写逻辑来检查给定字符串的最长子字符串,该字符串重复了2次或2次以上 前 字符串str=aabbcaaaaabbcaabccaabdab To find out Longest substring which repeated 2 or more times. output: aabbcaa 为了有效地做到这一点,你应该研究尝试 这里有一个链接到一个。特别是前缀树。这里有一个简单(但效率低下)的算法:循环所有可能的子字符串长度,从最大值到1。对于每个长度,将该

我只想编写逻辑来检查给定字符串的最长子字符串,该字符串重复了2次或2次以上

前 字符串str=aabbcaaaaabbcaabccaabdab

      To find out Longest substring which repeated 2 or more times.

      output:  aabbcaa

为了有效地做到这一点,你应该研究尝试

这里有一个链接到一个。特别是前缀树。

这里有一个简单(但效率低下)的算法:循环所有可能的子字符串长度,从最大值到1。对于每个长度,将该长度的所有子字符串放入字典中。如果你发现一个重复的,停止。它一定是最大的。以下是相应的C#代码:

公共静态字符串FindDuplicateSubstring(字符串s)
{
对于(int len=s.Length-1;len>0;len--){
var dict=新字典();

对于(int i=0;我知道你想这么做,但你试过了吗?现在看看我们的小讨论发生了什么。希望你现在能更好地理解:(@belisarius,是的,我看到了..我以后会更清楚的
public static string FindDuplicateSubstring(string s)
    {
        for (int len = s.Length-1; len > 0; len--) {
            var dict = new Dictionary<string, int>();
            for (int i = 0; i <= s.Length - len; i++) {
                string sub = s.Substring(i, len);
                if (dict.ContainsKey(sub)) return sub;
                else dict[sub] = i;
            }
        }
        return null;
    }
contacts.Sort();


for (int i=1; i <= contacts.Count-1; i++)
{
Console.WriteLine(contacts[ i ]);
Console.WriteLine(contacts[ i-1] );
if(contacts[ i ].ToString() == contacts[ i-1 ].ToString())
{
Console.WriteLine("Duplicate: "+contacts[ i ]);
}
}