首页 > 代码库 > 查找自身最长重复子串
查找自身最长重复子串
QString MaxSubString(QString inputString)
{
QString subString = "";
int strLen = inputString.size();
if (strLen <= 0)
{
return subString;
}
int maxSubStrLen = 0;
int tempLen = 0;
int subIndex = 0;
int i = 0;
int j = 0;
int k = 0;
while (i < strLen)
{
j = i+1;
while(j < strLen)
{
if (inputString.at(i) == inputString.at(j))
{
tempLen = 1;
for (k=1; j+k<strLen && inputString.at(i+k)==inputString.at(j+k); k++)
{
tempLen++;
}
if (tempLen > maxSubStrLen)
{
subIndex = i;
maxSubStrLen = tempLen;
}
j += tempLen;
}
else
{
j++;
}
}
i++;
}
for (i=0; i<maxSubStrLen; i++)
{
subString.append(inputString.at(subIndex+i));
}
return subString;
}
查找自身最长重复子串