解题思路
例如字符串“abababc”,最多连续出现的为ab,连续出现三次。要和求一个字符串中的最长重复子串区分开来,还是上面的字符串,那么最长的重复子串为abab。两个题目的解法有些类似,都用到了后缀数组这个数据结构。求一个字符串中连续出现的次数最多的子串,首先生成后缀数组例如上面的字符串为:
abababc
bababc
ababc
babc
abc
bc
c
可以看出第一个后缀数组和第三个后缀数组的起始都为ab,第5个后缀数组也为ab。可以看出规律来,一个字符串s,如果第一次出现在后缀数组i的前面,那么如果它重复出现,下一次出现应该在第i+len(s)个后缀数组的前面。
实现代码
#include <iostream>
#include <cstring>
#include <utility>
#include <string>
#include <vector>
using namespace std;
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));
}
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);
}
int main()
{
string str;
pair<int, string> rs;
while (cin>>str)
{
rs = fun(str);
cout<<rs.second<<":"<<rs.first<<endl;
}
return 0;
}
另一种思路:
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));
}
int count = 1;
int maxCount = 1;
string sub;
//i为子串的长度
for (int i = 1; i < len; i++)
{
for (int j = 0; j + i < len; j += 1)
{
int k = j;
count = 1;
while (k + i < len && subs[k].substr(0, i) == subs[k + i].substr(0, i))
{
++count;
k += i;
}
if (count > maxCount)
{
maxCount = count;
sub = subs[j].substr(0, i);
}
}
}
return make_pair(maxCount, sub);
}