POJ-2752 seek the Name, seek the Fame string problem KMP algorithm to find the same number of wood before and after the prefix

Source: Internet
Author: User

Title Link: https://cn.vjudge.net/problem/POJ-2752

Test instructions

Give a string the number of prefix strings with the same prefix string as the suffix string
Example: Alala
Output: A, Ala, Alala

Ideas

Think about it, the return value of Fail[len] is actually the maximum number of successful suffix strings.
After getting this suffix string, the string smaller than this string must still be included in this new suffix string.
Iteration can

Submission Process
AC
Code
  #include <cstring> #include <cstdio>const int Maxm=4e5+20;char p[maxm];int fail[    Maxm];void getfail (int m) {fail[0]=fail[1]=0;        for (int i=1; i<m; i++) {int j=fail[i];        while (J && P[j]!=p[i]) j=fail[j];    Fail[i+1]= ((p[i]==p[j))? j+1:0);    }}int Main (void) {int len, ANS[MAXM], kase=0; while (scanf ("%s", P) ==1 &&! ( p[0]== '. ' &&!        P[1]) {Getfail (Len=strlen (P))); int Size=len, ptr=1;        Ans[0]=len;        while (size>0) ans[ptr++]= (Size=fail[size]); for (int i=ptr-2; i>=0; i--) printf ("%d%c", ans[i], "\ n" [!!    I]); } return 0;  
Time
Memory Length Lang submitted
485ms 3840kB 547 g++ 2018-08-02 11:27:13

POJ-2752 seek the Name, seek the Fame string problem KMP algorithm to find the same number of wood before and after the prefix

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.