题解 | #最长重复子串#
最长重复子串
https://www.nowcoder.com/practice/4fe306a84f084c249e4afad5edf889cc
#include <algorithm>
class Solution {
public:
int solve(string a) {
// 利用一个循环依次遍历以哪个字符为首截取长度一直递增
int n = a.size();
int m = 0;
if(n == 0 || n == 1)return 0;
for(int i = 0; i < n; i++){
for(int j = 1; j <= (n - i)/2; j++){
if(2 * j > n - i) break;
string temp = a.substr(i, j);
string temp2 = a.substr(i + j, j);
//cout<<temp <<" temp"<<endl;
// cout<<temp2<<" temp2"<<endl;
if(temp == temp2) {
if(temp.size() *2 > m) m = temp.size() * 2;
}
}
}
return m;
}
};

