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

把字符串用后缀树的形式表现出来如下:

a b c a b c a b c d e .substr[0]

b c a b c a b c d e ....substr[1]

c a b c a b c d e .......substr[2]

a b c a b c d e ..........substr[3]

b c a b c d e .............substr[4]

c a b c d e ...............substr[5]

a b c d e .................substr[6]

b c d e ...................substr[7]

c d e .....................substr[8]

d e ........................substr[9]

e ..........................substr[10]

可以观察到,若存在连续出现的字串,则满足 substr[0].substr(i,j-i) == substr[j].substr(0,j-i),例如上例中的

substr[0].substr(0,3-0) == substr[3].substr(0,3-0)

我们换一种方式来看,不需要生成后缀组,但思想还是一样的。

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

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

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

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

代码:

代码中str.substr(pos2,offset)其实相当于后缀组的substr[pos2].substr(0,offset)

把字符串写成后缀组其实相当于站在不同的位置往后看这个数组,所以其实并不需要额外增加存储空间来生成后缀组。

#include <iostream>

#include <string>

using namespace std;



void main(){

    string str = "abcabcabcccccdefefefefefef";



    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;



}

 

 

你可能感兴趣的:(字符串)