问题:求一个字符中连续出现次数最多的子串,如abcbcbcabc 中,连续出现的字符串为bc,连续三次。
方法1:把字符串用后缀树的形式表现出来如下:
a b c b c b c a b c .substrs[0]
b c b c b c a b c.....substrs[1]
c b c b c a b c .......substrs[2]
b c b c a b c ..........substrs[3]
c b c a b c .............substrs[4]
b c a b c ................substrs[5]
c a b c ...................substrs[6]
a b c ......................substrs[7]
b c .........................substrs[8]
c ............................substrs[9]
代码如下:
# include<iostream>
# include<string>
# include<vector>
using namespace std;
pair<int,string> fun(const string& str)
{
vector<string> substrs;
int maxcount = 1;
int 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)==substrs[j].substr(0,j-i))
{
++count;
for(int k=j+(j-i);k<len;k+=j-i)
{
if(substrs[i].substr(0,j-i)==substrs[k].substr(0,j-i))
++count;
else
break;
}
if(count>maxcount)
{
maxcount=count;
substr = substrs[i].substr(0,j-i);
}
}
}
}
return make_pair(maxcount,substr);
}
int main()
{
string str;
pair<int,string>rs;
while(cin>>str)
{
rs=fun(str);
cout<<rs.second<<':'<<rs.first<<'\n';
}
return 0;
}
方法2:不需要生成后缀组,但思想还是一样:
代码:
代码中str.substr(pos2,offset)其实相当于后缀组的substr[pos2].substr(0,offset)
把字符串写成后缀组其实相当于站在不同的位置往后看这个数组,所以其实并不需要额外增加存储空间来生成后缀组。
#include <iostream>#include <string>
using namespace std;
void main()
{
string str = "abcbcbcabc";
int len = str.length();
int maxCount = 0;
string longest = "";
for(int pos1 = 0; pos1 < len; pos1++)
for(int pos2 = pos1 + 1; pos2 < len; pos2++)
{
if(str.substr(pos1,pos2-pos1) == str.substr(pos2,pos2-pos1))
{
int offset = pos2-pos1;
int count = 2;
for(int k = pos2 + offset; k <= len; k += offset)
{
if(str.substr(pos1,offset) == str.substr(k,offset))
{
count += 1;
}
else
{
break;
}
}
if(count > maxCount)
{
maxCount = count;
longest = str.substr(pos1,offset);
}
}
}
cout << longest << "," << maxCount << endl;
}