2019 力扣杯-全国高校春季编程大赛 最长重复子串

给定字符串 S,找出最长重复子串的长度。如果不存在重复子串就返回 0

示例 1:

输入:"abcd"
输出:0
解释:没有重复子串。

示例 2:

输入:"abbaba"
输出:2
解释:最长的重复子串为 "ab" 和 "ba",每个出现 2 次。

示例 3:

输入:"aabcaabdaab"
输出:3
解释:最长的重复子串为 "aab",出现 3 次。

示例 4:

输入:"aaaaa"
输出:4
解释:最长的重复子串为 "aaaa",出现 2 次。

提示:

  1. 字符串 S 仅包含从 'a' 到 'z' 的小写英文字母。
  2. 1 <= S.length <= 1500

https://www.nowcoder.com/questionTerminal/859d3e13ebb24e73861e03141bbe9cfb?from=14pdf

老题了

class Solution {
public:
    int longestRepeatingSubstring(string S) {
        string maxstr,str = S;
        for (int i = 0; i < str.size(); i++){
            for (int j = 0; j < i; j++){
                string temp;
                int k = j;
                int m = i;
                cout<<m<<"---"<<k<<endl;
                while (str[m] == str[k] && i<str.size() && k<str.size())
                {
                    cout<<m<<" "<<k<<endl;
                    m++; k++;
                }
                cout<<"---"<<endl;
                temp = str.substr(j, k - j);
                
                if (temp.size()>maxstr.size())
                    maxstr = temp;
                }
        }
        return maxstr.size();
    }
};

猜你喜欢

转载自www.cnblogs.com/yinghualuowu/p/10746664.html