POJ 2752 seek the Name, seek the Fame "KMP Algorithm analysis record"

Source: Internet
Author: User
Tags integer numbers

Seek the Name, seek the Fame
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 14106 Accepted: 7018

Description

The little cat is so famous, which many couples tramp over Hill and Dale to Byteland, and asked the little cat to give name s to their newly-born babies. They seek the name, and at the same time seek the fame. In order-to-escape from such boring job, the innovative little cat works out a easy but fantastic algorithm:

Step1. Connect The father ' s name and the mother ' s name, to a new string s.
Step2. Find a proper Prefix-suffix string of s (which is isn't only the prefix, but also the suffix of s).

Example:father= ' ala ', mother= ' La ', we have S = ' ala ' + ' la ' = ' alala '. Potential prefix-suffix strings of S is {' A ', ' ala ', ' Alala '}. Given the string S, could the little cat to write a program to calculate the length of possible prefix-suffix str Ings of S? (He might thank you by giving your baby a name:)

Input

The input contains a number of test cases. Each test case occupies a, contains the string S described above.

Restrictions:only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

Output

For each test case, output a single line with an integer numbers in increasing order, denoting the possible length of the new Baby ' s name.

Sample Input

Ababcababababcababaaaaa

Sample Output

2 4 9 181 2 3 4 5

Test instructions: asks for the length of all substrings with the same prefix and suffix
The first thing to know is what prefixes and suffixes
1, prefix: Starting from the first bit to take a substring of the character string, such as the string abcd its prefix is: A,AB,ABC,ABCD note ABCD is also its prefix
2, suffix: From the last one to start sequentially to take the characters in the string of substrings, such as ABCD suffix: D,CD,BCD,ABCD
We ask for the mismatch function f[] Its mismatch value f[i] is the prefix suffix from 0 to i the same maximum substring length as

  

To find a mismatch function:
Method One, let's ask the prefix of this set of data (that is, the value of the mismatch function) f[0]=0;
(regardless of the entire string itself)
A's prefix is empty (1 position f[1]=0)
AB prefix is a, suffix B is unequal (f[2]=0)
The prefix for ABC is a,ab suffix bc,b unequal (f[3]=0)
ABCD prefix for a,ab,abc suffix bcd,cd,d not equal (f[4]=0)
The prefix for the ABCDA is A,AB,ABC,ABCD, and the suffix is bcda,cda,da,a with the same prepended a and a length of 1 (f[5]=1)
And so get f[6]=2,f[7]=0;
Method Two,

Another method to find the mismatch function

Stipulation f[0]=f[1]=0 (mismatch function No. 0 and 1th for BI et 0)

A set of data to explain how to ask:
0 1 2 3 4 5 6 7
A b c d a b D
0 0
The No. 0 and 1th bits have been determined to start from 2nd, and we need to look at the previous one when we ask for C.
Character and the mismatch value of the previous character (F[i] value), the previous one is the B mismatch value is 0, we'll see
The No. 0 bit represents the same character as B, the same f[2]=f[1]+1, and the f[2]=0
This is different so f[3]=0, when the data becomes:
0 1 2 3 4 5 6 7
A b c d a b D
0 0 0

3rd place, let's compare his previous, for C mismatch value of 0 We still compare the previous C of D with the No. 0 place.
C and A are different so f[3]=0; the data becomes:

0 1 2 3 4 5 6 7
A b c d a b D
0 0 0 0

Similarly f[4]=0

0 1 2 3 4 5 6 7
A b c d a b D
0 0 0) 0 0

5th place the former is a mismatch value of 0 at this time the fifth bit A is equal to the No. 0 bit a so f[5]=f[4]+1=1

0 1 2 3 4 5 6 7
A b c d a b D
0 0 0 0 0 1

6th place the former is a B mismatch value of 1 This time the sixth bit B equals 1th bit B so f[6]=f[5]+1=2
0 1 2 3 4 5 6 7
A b c d a b D
0 0 0 0 0 1 2

Similarly the seventh place f[7]=0;

Here we follow the method to continue to explain

/** 0 1 2 3 4 5 6 7  //string character corresponding position * a B c d a b d    //String * 0 0 0 0 0 1 2 0  //mismatch value of string *//**  when I equals 1 o'clock that string position is 0-> ; 0 at this time the mismatch value is 0 because no *  prefix same as I and so on 2 o'clock also, and so on, when I equals *  5 o'clock string corresponding position is 0->4 (length 5) At this time f[5]=1 because *  has the same prefix a and length of 1, When I equals 6 o'clock the string corresponds to the position *  0->5 at this time f[6]=2 because there is the same prefix AB *  This verifies what we said above *  Note: Here our prefix does not consider the entire string itself, and *       The length of our I representation when the substring of the prefix is asked  

So we want to ask that the length of the same substring of the prefix is only required for the value of the corresponding positional mismatch function f[i].

Here is an example of two sets of data in the topic:

/**0 1 2 3 4 5 6 7 8 9 18*a b A b c A b A B a B a B  c  a  b  a
   b*0 0 0 1 2 0 1 2 3 4  3  4  3  4  5  6  7  8  9*f[18]=9  f[9]=4  f[4]=2< c31/>f[2]=0* so we only need to let k=f[k], and then take f[k] value until *k equals 0 to get results **0 1 2 3 4 5*a A A a a*0 0 1 2 3 the same f[5]=4 f[4]=3 f[3]=2  f[2]=1  f[1]=0*/

AC Code:

#include <stdio.h> #include <string.h> #define MAX 400100char str[max];int f[max];int dp[max];int len;void Getfail () {int i,j;f[0]=f[1]=0;for (i=1;i<len;i++) {j=f[i];while (j&&str[i]!=str[j])    j=f[j];f[i+1]= str[i]==str[j]?j+1:0;}} void KMP () {int k=len;    Intn=0;while (F[k]) {dp[n++]=f[k];k=f[k];} for (int i=n-1;i>=0;i--) printf ("%d", Dp[i]);p rintf ("%d\n", Len);} int main () {int i;while (scanf ("%s", str)!=eof) {Len=strlen (str); Getfail (); KMP ();} return 0;}

  

POJ 2752 seek the Name, seek the Fame "KMP Algorithm analysis record"

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.