Poj--2752seek the Name, seek the Fame (KMP to find the same string before and after)

Source: Internet
Author: User
Tags integer numbers

Seek the Name, seek the Fame
Time limit:2000ms Memory limit:65536k
Total submissions:16035 accepted:8158
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


Ababcababababcabab
Aaaaa
Sample Output


2 4 9 18
1 2 3) 4 5
Source


POJ Monthly--2006.01.22,zeyuan Zhu

Given a string s, the length of a substring that is both a prefix and a suffix from small to large output s.
This problem is very simple, borrow the next array of the KMP algorithm, set S length is N,
The S-string itself must satisfy the condition. Other substrings that satisfy the condition have a feature,
The last character of the substring must be the same as the last character of S.
This is when the next array works.
From the n-1 bit both the last one starts rolling back, if s[next[n-1]] = = S[n-1],
The substring s[0,1,2,..., next[n-1]] is a substring that satisfies the condition.
Then Judge s[next[next[n-1]] [= S[n-1] is set up, so keep rolling back,
Until next[next[.....next[n-1]] = =-1. Save the answer from the big to the small, then from small to large output can.


#include <iostream> #include <cstdio> #include <cstring>using namespace Std;int next[400005];char str [400005];int ans[400005];int cnt;int len;void getNext () {next[0] = -1;int i = 0, j = -1;while (i < len) {if (j = =-1 | | s Tr[i] = = Str[j]) {++i;++j; Next[i] = j;} else J = Next[j];}} int main () {while (scanf ("%s", str)! = EOF) {len = strlen (str); GetNext (); cnt = 0;int T = next[len-1];while (t! =-1) {if (s Tr[t] = = str[len-1]) ans[cnt++] = T+1;t = next[t];//jumps back to the previous position matching the last character}for (int i = cnt-1; I >= 0; i) {printf ("%d ", Ans[i]);} printf ("%d\n", Len);} return 0;}


Poj--2752seek the Name, seek the Fame (KMP to find the same string before and after)

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.