找出一行字符串中的连续出现次数最多的字串
#include <iostream>
#include <string>
#include <vector>
using namespace std;
//先找出字符串的所有子串
//
void findTheMostSame(string str){
int length=str.length();
vector<string> substrs, results;
int i=0,count=1,maxcount=2;
string t ;
for (int i=0; i<length; i++) {
substrs.push_back(str.substr(i,length-i));
}//不采集的话,后面再采集就很麻烦
for ( i=0; i<length; i++) {
for (int j=i+1; j<length; j++) {//明白初始条件j-i=1,然后j++的条件的含义么,得看往下看j和i怎么用才会明白
count=1;//重新计数
if(substrs[i].substr(0,j-i)==substrs[j].substr(0,j-i)){//明白了么,连续子串对比
//cout<<substrs[j].substr(0,j-i)<<endl;
count++;
for (int d=j-i,k=j+d;k<length ;k+=d ) {
if (substrs[k].substr(0,d)==substrs[i].substr(0,d)) {
count++;
}
else{
break;
}
}//k
}
if (count>=maxcount) {
maxcount=count;
cout<<substrs[i].substr(0,j-i)<<endl;
cout<<"次数"<<maxcount<<endl;
}
}//j
}
substrs.clear();
for (int i=0; i<results.size(); i++) {
cout<<results[i]<<endl;
}
}
int main(int argc, const char * argv[])
{
// insert code here...
findTheMostSame("232323232323267681561648646484618681656606");
return 0;
}