HDU 2594-simpsons ' Hidden talents (KMP)

Source: Internet
Author: User

Topic Links:

http://acm.hdu.edu.cn/showproblem.php?pid=2594

Title Description:

Given two strings s1,s2, ask if there is a longest substring, so that the substring is both a S1 prefix and a s2 suffix.

Exercises

Since it is required s1 prefix and S2 suffix, then as long as the S1 and S2 merged into a string str, and then a wave of the string KMP to know whether the prefix of the topic requirements exist.

The most convenient way to merge strings is string, so use C + + to do this problem.

However, it is important to note that if the content of S1 and S2 is the same, it will eventually get more than S1 and S2 substring, which is obviously not possible, so in the final output to compare the substring length and s1, s2 length.

Code:

1#include <iostream>2#include <cmath>3#include <cstdio>4#include <cstring>5#include <string>6#include <vector>7#include <algorithm>8 using namespacestd;9 Ten #definell Long Long One #defineINF 0x3f3f3f3f3f A #defineMAX 100100 -  - intFail[max]; the  - voidGetfail (stringstr) - { -     intLen =str.size (); +fail[0] =0; -     intK =0; +      for(inti =1; i < Len; ++i) { A          while(k >0&& Str[i]! =Str[k]) atK = fail[k-1]; -         if(Str[i] = =Str[k]) -k++; -Fail[i] =K; -     } - } in  - intMain () to { +     //freopen ("Debug\\in.txt", "R", stdin); -     //freopen ("CON", "w", stdout); the     intI, J, K; *     strings1, S2; $      while(Cin >> S1 >>S2) {Panax Notoginsengmemset (fail,0,sizeof(fail)); -         stringstr = S1 +S2; the getfail (str); +  A         intans = fail[str.size ()-1]; the         stringtmp; +         intMINW =min (s1.size (), s2.size ()); -          for(i =0; I < ans && i < minw; ++i) {//"I < MINW" is to prevent substring length from exceeding the original string $TMP + =Str[i]; $         } -         if(!ans) -cout << ans <<Endl; the         Else -cout << tmp <<" "<< min (minw, ans) <<Endl;Wuyi     } the     return 0; - } Wu  - /* About Input: $ ABCABCABCABC - ABCABCABCABCABC - ABCABC - ABC A Clinton + Homer the Riemann - Marjorie $  the Output: the ABCABCABCABC the ABC 3 the 0 - Rie 3 in  */

HDU 2594-simpsons ' Hidden talents (KMP)

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.