Maximum continuous replay substring

Source: Internet
Author: User

Problem description: Find a substring in a string that consists of a string "continuous repetition" and find the sub-string consisting of the "Maximum number of consecutive repetitions. If multiple results exist, find the smallest dictionary order.

For example, for the string abababcc, "ababab" is the maximum continuous repeated substring, where "AB" is repeated three times.

Another example is bbbaaa. The answer is "AAA" and "A" is repeated three times. Although "BBB" is also composed of "B" three times in a row, the dictionary is ordered "AAA" <"BBB ".

 

Problem solving:

This problem can be considered as a sub-problem (see the previous blog), and then traverse the length of the entire string l, call "subproblem" for each substring with the length of L to obtain the next array of each substring, and finally obtain the result with the highest number of repetitions (if unique, otherwise, dictionary sort substring ).

 

Such a time is very complex, but I really don't have any efficient algorithms ......

# Include <iostream> <br/> # include <cstdlib> <br/> # include <cstdio> <br/> # include <cstring> <br/> using namespace STD; <br/> int next [100010]; <br/> char s [100010]; <br/> char max_s [100010]; <br/> int max_times; <br/> void get_next (int n, char * s) <br/>{< br/> int I = 0; <br/> next [I] =-1; <br/> Int J = next [0]; <br/> while (I <= N) <br/> {<br/> If (j =-1 | s [I] = s [J]) <br/>{< br/> I ++; <br/> J ++; <br/> next [I] = J; <br/>}< Br/> else <br/> J = next [J]; <br/>}< br/> void solve (INT St, int en) <br/>{< br/> memset (next, 0, sizeof (next); <br/> int Len = en-ST + 1; <br/> get_next (Len, S + st); <br/> int T = len-next [Len]; <br/> int times = 1; <br/> If (LEN % T = 0) <br/>{< br/> times = Len/T; <br/>}< br/> else <br/> times = 1; <br/> If (max_times = Len/T) <br/>{< br/> char * tmp_s = new char [t + 1]; <br/> memset (tmp_s, 0, sizeof (tmp_s )); <br/> S Trncpy (tmp_s, S + En + 1-T, T); <br/> If (strcmp (max_s, tmp_s)> 0) <br/>{< br/> memset (max_s, 0, sizeof (max_s); <br/> strncpy (max_s, S + En + 1-T, t ); <br/>}< br/> Delete [] tmp_s; <br/>}< br/> If (max_times <times) <br/> {<br/> max_times = times; <br/> memset (max_s, 0, sizeof (max_s); <br/> strncpy (max_s, S + En + 1-T, T); <br/>}< br/> int main () <br/>{< br/> scanf ("% s", S); <br/> int c = 1; <br/> while (s [0]! = '#') <Br/>{< br/> int I, j; <br/> int L = strlen (s ); <br/> for (I = 0; I <L; I ++) <br/> for (j = I; j <L; j ++) <br/>{< br/> If (J-I + 1 <max_times) <br/> continue; <br/> solve (I, j ); <br/>}< br/> printf ("case % d:", c); <br/> for (I = 0; I <max_times; I ++) <br/> printf ("% s", max_s); <br/> cout <Endl; <br/> memset (S, 0, sizeof (s )); <br/> memset (next, 0, sizeof (next); <br/> memset (max_s, 0, sizeof (max_s); <br/> max_times = 0; <br/> C ++; <br/> scanf ("% s", S); <br/>}< br/>

Contact Us

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.