Subject description:
The largest number of occurrences of substrings request string appears.
Such as string abcbcbcabc, largest number of occurrences substring is bc, occurrences 4
Idea: using the suffix array:
abcbcbcabc 0th
bcbcbcabc 1st
cbcbcabc second
bcbcabc third
cbcabc fourth
bcabc fifth
cabc sixth
abc seventh
bc 8th
c 9th
Process: Remove the start of the array 0 a, b, and then comparing the first array, found unequal, then take ab, abc, abcb .... After taking this trip, and from a first suffix array He began to take, take b, bc, bcb, bcbc ...
#include<iostream>
#include<string>
#include<vector>
using namespace std;
pair<int, string> fun(const string &str)
{
vector<string> substrs;
int len = str.length();
string substring;
int maxcount(0);
//后缀数组
cout << "the string is:" << str << endl;
cout << "the substrings are as follows:" << endl;
for (int i = 0; i < len; ++i)
{
substrs.push_back(str.substr(i));
cout << substrs[i] << endl;
}
cout << "--------------the answer------------" << endl;
for (int i = 0; i < len; ++i)
{
for (int j = 1; j <= len; j++) {
int count = 1;
int sublen = j;
for (int k = i + 1; k < len; k++) {
if (substrs[k].length() < sublen) {
break;
}
//cout << substrs[i].substr(0, sublen) << endl;
//cout << substrs[k].substr(0, sublen) << endl;
string str1 = substrs[i].substr(0, sublen);
string str2 = substrs[k].substr(0, sublen);
//cout << "比较结果:" << str1.compare(str2) << endl;
//cout << "i = " << i << " sublen = " << j << " k = " << k << endl;
if (str1.compare(str2)==0)
{
++count;
}
//cout << "count = " << count << endl;
}
if (count > maxcount||(count == maxcount && sublen > substring.length()))
{
maxcount = count;
substring = substrs[i].substr(0, sublen);
}
}
}
return make_pair(maxcount, substring);
}
int main()
{
string str = "ababcababcabcab";
auto res = fun(str);
cout << "the max count is:" << res.first << endl;
cout << "the matched substring is:" << res.second << endl;
return 0;
}
Output:
the string is:ababcababcabcab
the substrings are as follows:
ababcababcabcab
babcababcabcab
abcababcabcab
bcababcabcab
cababcabcab
ababcabcab
babcabcab
abcabcab
bcabcab
cabcab
abcab
bcab
cab
ab
b
--------------the answer------------
the max count is:6
the matched substring is:ab
References:
[1] a string seeking the largest number of consecutive substring https://blog.csdn.net/qq_22080999/article/details/81143555
[2] [Title] Algorithm brush up to the number of times a string of consecutive sub string https://blog.csdn.net/Neo_dot/article/details/80559744