找出全部最长连续反复子串及其个数

问题描写叙述:
找出字符串中所以最长连续反复子串及其个数
比方:输入:123234,最大连续反复字符串为23,个数为2
           输入:5555,最大连续反复字符串为555,个数为2
           输入:aaabbb 最大连续反复字符串为aa,个数为2;和bb,个数为2
必须存在反复的字符串才算,仅仅出现一次的不算。可能存在多个同样长度的不同字符串,比方aaabbb。

分析:最直接的想法是利用两个指针循环遍历比較全部可能的子串,记录下全部子串长度,然后找到全部最大连续子串及其个数。时间复杂度为O(n^2)。

在网上看到一种利用后缀树组的方式来处理的方法,其思想主要是将字符串的全部后缀子串用数组记录下来,然后将全部子串按字典序排序,然后依次比較相邻的字符串就可以统计出全部可能的连续反复子串,降低了非常多不必要的比較。时间复杂度主要是字符串排序的复杂度,为O(nlogn)。主要代码例如以下:

#include<iostream>
#include<string.h>
#include<map>
#include<algorithm>

using namespace std;


struct scmp

{
    bool operator()(const char *s1, const char *s2) const
    {
        return strcmp(s1,s2)<0;
    }
};


int mycmp(const void *p1, const void *p2)
{
	return strcmp(*(char **)p1, *(char **)p2);
}


int comlen(char *p,char *q)
{
	int len=0;
	while(*p&&*q&&*p++==*q++)
		len++;
	return len;
}



int main()
{
	char s[50];
	char *a[51];
	int cnt[50]={0};
	map<char*,int,scmp> chmap;
	int i,n,max=0;
	cin>>s;
	n=strlen(s);
	if(n==0)
		return 0;
	for(i=0;i<n;i++)
		a[i]=&s[i];
	a[n]=0;
	qsort(a,n,sizeof(char *),mycmp);

	for(i=0;i<n-1;i++)
	{
		int temp=comlen(a[i],a[i+1]);
		if(max<temp)
		{
			max=temp;
		}
		cnt[i]=temp;
	}
	for(i=0;i<n;i++)
		if(cnt[i]==max)
		{						
			char *subs=new char[50];
			strncpy(subs,a[i],max);
			subs[max]='';
			map<char*,int,scmp>::iterator it=chmap.find(subs);
			if(it!=chmap.end())
			{
				it->second++;
			}
			else
				chmap.insert(make_pair(subs,2));

		}
	cout<<"Result:"<<endl;
	for(map<char*,int,scmp>::iterator i=chmap.begin();i!=chmap.end();i++)
		cout<<i->first<<" "<<i->second<<endl;

	for(map<char*,int,scmp>::iterator i=chmap.begin();i!=chmap.end();i++)
		delete i->first;

	return 0;
}


【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/llguanli/p/8706992.html