文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>字符串连续出现次数最多的子串

字符串连续出现次数最多的子串

时间:2010-07-05  来源:静止的流水


#include "stdafx.h"
#include<iostream>
#include<string>
#include<math.h>
#include<vector>

using namespace std;
pair<int,string> getMaxSub(string &str){
    int len = str.length();
    vector<string> substrs;
    for(int i = 0 ; i < len ; i ++){
        cout<<str.substr(i,len-i)<<endl;
        substrs.push_back(str.substr(i,len-i));
    }
    int maxcount = 1;
    int count = 1;
    string substrtmp;
    for(int 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;
                    substrtmp = substrs[i].substr(0,j-i);
                }
            }
        }
    }
    return make_pair(maxcount,substrtmp);
}
int _tmain(int argc, _TCHAR* argv[])
{
    string str("abcabcabc");
    pair<int,string> rs;
    rs = getMaxSub(str);
    cout<<rs.second<<rs.first<<endl;
    return 0;
}


abcabcabac

bcabcabc

cabcabc

abcabc

bcabc

cabc

abc

bc

c

循环比较第i个子串和j个子串的前面j-i个字符的异同,相同继续循环比较第k=j+j-i到第len,以j-i为步长的前面j-i个字符的异同。

相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载