求一个字符串中连续出现次数最多的子串

参考:http://www.cnblogs.com/elaron/p/3330026.html

#include<iostream>
#include<string>
using namespace std;
int main(void)
{
        int pos1,pos2,pos3,offset,temp=1,count=0;
        string str="bcdcdcdcdcdcdbcdasfabababds";
        string strTemp="";
        int len=str.length();
        for(pos1=0;pos1<len;pos1++)
        {   
                for(pos2=pos1+1;pos2<len;pos2++)
                {   
                        if(str.substr(pos1,pos2-pos1)==str.substr(pos2,pos2-pos1))
                        {   
                                offset=pos2-pos1;
                                count=2;
                                for(pos3=pos2+offset;pos3<len;pos3+=offset)
                                {   
                                        if(str.substr(pos1,offset)==str.substr(pos3,offset))
                                                count++;
                                        else
                                                break;
                                }   
                                if(count>temp)
                                {   
                                        temp=count;
                                        strTemp=str.substr(pos1,offset);
                                }   
                        }   
                }   
        }   
        cout<<strTemp<<"最大次数为"<<temp<<endl;
        return 0;
}

 程序猿必读

原文地址:https://www.cnblogs.com/longzhongren/p/4423068.html