HDU 1841 find the shortest common superstring (flexible use of KMP)

Source: Internet
Author: User
Ind the shortest common Superstring

Time Limit: 1000/1000 MS (Java/others) memory limit: 65535/65535 K (Java/Others)
Total submission (s): 1149 accepted submission (s): 289


Problem descriptionthe shortest common superstring of 2 strings S1 and S2 is a string s with the minimum number of characters which contains both S1 and S2 as a sequence of consecutive characters. for instance, the shortest common Superstring
Of "Alba" and "Bacau" is "albacau ".
Given two strings composed of lowercase English characters, find the length of their shortest common superstring.


Inputthe first line of input contains an integer number t, representing the number of test cases to follow. each test case consists of 2 lines. the first of these lines contains the string S1 and the second line contains the string S2. both
Of these strings contain at least 1 and at most 1.000.000 characters.


Outputfor each of the T test cases, in the order given in the input, print one line containing the length of the shortest common superstring.


Sample Input

2albabacauresitamures
 


Sample output

78
Here are two strings. Finding a minimum length string can include both strings. Output the problem-solving idea of its length: Start to directly connect the two strings into two long strings, and then calculate its minimum cyclic section. Finally, WA looked at discuss and found that he could not pass the data of abcde bcd, mainly because one of his own strings contained the other. In this case, it would not be the first and last links. Therefore, we need to pre-process and determine whether the two strings are directly contained. If yes, we will output them directly. I didn't think of this method at first. I directly used a solution like 3450. I tried to enumerate small strings and then matched them. However, it times out. Because the length is 10 ^ 5 or relatively large, then the time of O (N ^ 2) exceeds 10 ^ 9.
Address: Find the shortest common Superstring
AC code:
/* You cannot directly find the next array. This data may exist. It is not necessarily the abcde bcd connected at the beginning and end. The answer is 5 abcde qbc. The answer is 8 ABCDE ebcd. The answer is output 8, if this parameter is not included, it is enough to connect to next */# include <iostream> # include <cstring> # include <string> # include <cstdio> using namespace STD; char S1 [1000005], S2 [1000005], S3 [2000005], S4 [2000005], s [1000005]; int len1, len2, Len, next [2000005]; void getnext (char * P, int Len) // P is the mode string {int I, j; next [0] = 0, next [1] = 0; for (I = 1; I <Len; I ++) {J = next [I]; while (J & P [I]! = P [J]) J = next [J]; If (P [I] = P [J]) next [I + 1] = J + 1; else next [I + 1] = 0 ;}} int KMP (char * P, int lenp, char * a, int Lena) {int I, j = 0; for (I = 0; I <Lena; I ++) {While (J & A [I]! = P [J]) J = next [J]; if (a [I] = P [J]) J ++; If (j = lenp) return 1;} return 0;} int main () {int t; scanf ("% d", & T); getchar (); While (t --) {gets (S1); gets (S2); len1 = strlen (S1), len2 = strlen (S2); If (len1> len2) {strcpy (S, S1 ); strcpy (S1, S2); strcpy (S2, S);} len1 = strlen (S1), len2 = strlen (S2); getnext (S1, len1 ); if (KMP (S1, len1, S2, len2) // S2 contains S1 printf ("% d \ n", len2); else {strcpy (S3, S1 ); strcat (S3, S2); // S3 = S1 + S2; strcpy (S4, S2); strcat (S4, S1); // S4 = S2 + S1; len = len1 + len2; int res; getnext (S3, Len); Res = len-next [Len]; // the smallest loop section, which can be connected to getnext (S4, Len ); if (RES> (LEN-next [Len]) RES = len-next [Len]; If (RES <len2) // The length cannot be less than two res = len2; printf ("% d \ n", Res) ;}} return 0 ;}

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.