1.首先明白什么是子串,其二注意题目说的是连续出现次数最多的字串。
2.比如字符串abcbcbcabcdbc,这个连续出现次数最多的子串是bc,注意连续出现次数是3次,最后那个bc不能算上,因为它没有连续出现。
3.对于一个字符串abcbcbcabcdbc,我们如何找到连续出现次数最多的呢?
以以上字符串为例,先从第一个字符a为起点开始,先只是看该"a",扫苗一圈发现并没有以该"a"连续出现的字符串(注意该"a"只是指的索引0位置的“a”,不算后面出现的"a");继续,从下一个位置"b"开始重新扫描经过扫面得出bc连续出现三次;继续,从"c开始扫描"得出cb出现2次。。。然后以此类推。得出bc为连续出现次数最多的字串
代码附上
#include<string>
#include<iostream>
#include<vector>
using namespace std;
pair<int, string> fun(const string& str)
{
vector<string> substrs;
int maxcount = 1, count = 1;
string substr;
int i, len = str.length();
for (i = 0; i < len; i++)
{
substrs.push_back(str.substr(i,len-i));
}
for (i = 0; i < len; i++)
{
for (int j = i + 1; j < len; j++)
{
count = 1;
if (substrs[i].substr(0, j - i) == sub