题解 | #最长重复子串#
最长重复子串
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;
}
};
查看18道真题和解析