题目描述
求串的最长重复子串长度(子串不重叠)。例如:abcaefabcabc的最长重复子串是串abca,长度为4。
输入
测试次数t
t个测试串
输出
对每个测试串,输出最长重复子串长度,若没有重复子串,输出-1.
输入输出样例
输入样例1 <-复制
3
abcaefabcabc
szu0123szu
szuabcefg
输出样例1
4
3
-1
AC代码
#include<iostream>
#include<string>
using namespace std;
int caozuo(string s1)
{
string s2, s3;
int len1 = s1.length();
int max = len1 / 2;
int len2,i, j;
int qi = 0;
for (len2 = max; len2 > 0; len2--)
{
qi = 0;
while (qi + len2 * 2 <= len1)
{
s2 = s1.substr(qi, len2);
for (i = qi + len2; i + len2 <= len1; i++)
{
s3 = s1.substr(i, len2);
if (s2 == s3)
return len2;
}
qi++;
}
}
return -1;
}
int main()
{
int t;
cin >> t;
string s1;
while(t--)
{
cin >> s1;
cout << caozuo(s1) << endl;
}
}
(by 归忆)