题解 | #最长重复子串#

最长重复子串

http://www.nowcoder.com/practice/4fe306a84f084c249e4afad5edf889cc

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     * 
     * @param a string字符串 待计算字符串
     * @return int整型
     */
    int solve(string a) {
        // write code here
        int n = a.length();
        if (n == 0 or n == 1) return 0;
        int ans = 0;
        function<void(int)> dfs = [&](int l) {
            if (l > n) return;
            for (int i = 0; i < n; i++) {
                if (i + l > n) break;
                string s1 = a.substr(i, l / 2);
                string s2 = a.substr(i + l / 2, l / 2);
                if (s1 == s2) {
                    ans = l;
                    break;
                }
            }
            dfs(l + 2);
        };
        dfs(2);
        return ans;
    }
};
全部评论

相关推荐

02-20 16:18
中南大学 C++
念~楠:哥们哥们,我帮你改简历,把rank删了,省的面试问你成绩那么好为什么留本校,银牌改金牌,lee改成codeforce也可以说自己id是tourist,项目和实习经历丢给大模型帮你改的像一点,多少有点东西,写点自己技术栈,这样比较好。这样改完以后offer乱收
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务