题目:求给定字符串中连续出现次数最多的子串。
例:abcbcbcabc,bc出现次数最多,为3次
分析:
1、将后缀数组依次存入字符串数组中
2、依次将第i个字符串的长度为j-i的子串与第j个字符串相比,若相等,则比较j字符串剩下有几个子串与之相等
3、记录每个子串出现的次数,并比较,保留最大的
pair<int, string> fun(const string &str)
{
vector<string> subs;
int len = str.size();
for (int i = 0; i < len; i++)
{
subs.push_back(str.substr(i, len - i));
}
int count = 1;
int maxCount = 1;
string sub;
for (int i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
count = 1;
if (subs[i].substr(0, j - i) == subs[j].substr(0, j - i))
{
++count;
//j-i为子串长度
for (int k = j + j - i; k < len; k += j - i)
{
if (subs[i].substr(0, j - i) == subs[k].substr(0, j - i))
{
++count;
}
else
{
break;
}
}
if (count > maxCount)
{
maxCount = count;
sub = subs[i].substr(0, j - i);
}
}
}
}
return make_pair(maxCount, sub);
}