Find all the longest continuous repeated substrings and their numbers

Source: Internet
Author: User
Problem description: Find the maximum number of consecutive repeated substrings in a string, for example, input: 123234, maximum continuous repeated string: 23, Number: 2 input: 5555, maximum continuous repeated string: 555, the number is 2 input: the maximum number of consecutive repeated aaabbb strings is AA, the number is 2; and BB, the number is 2 must have repeated strings, only appear once is not counted. There may be multiple different strings of the same length, such as aaabbb.
Analysis: the most direct idea is to use two pointers to traverse and compare all possible substrings, record the length of all substrings, and then find all the maximum continuous substrings and their numbers, the time complexity is O (n ^ 2 ). On the Internet, we can see a method to deal with suffix tree groups. The main idea is to record all suffix substrings of a string with arrays, and then sort all substrings in Lexicographic Order, then compare adjacent strings in sequence to calculate all possible consecutive repeated substrings, reducing unnecessary comparisons. the time complexity is mainly the complexity of string sorting, Which is O (nlogn ). The main code is as follows:
`#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]='\0';map<char*,int,scmp>::iterator it=chmap.find(subs);if(it!=chmap.end()){it->second++;}elsechmap.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;}`

Related Keywords:

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

• Sales Support

1 on 1 presale consultation

• After-Sales Support

24/7 Technical Support 6 Free Tickets per Quarter Faster Response

• Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.