POJ 2752+kmp+ uses next array properties to find all the same prefixes and suffixes

Source: Internet
Author: User

Topic Link: Click to enter
This topic requires the length of all the same prefixes and suffixes. We can take advantage of the nature of the next array in the KMP algorithm, which is continuously mismatch at the point of Next[len, so that all the same prefix lengths can be obtained. Also note that the length of the whole string can also be regarded as a legal solution.

The code is as follows:

#include <iostream>#include <cstdio>#include <cstring>using namespace STD;Const intmaxn=400000+ -;CharSTR[MAXN];intNEXT1[MAXN];intans[maxn],cnt;voidKMP_NEXT1 (CharX[],intMintNext1[]) {intI,j; j=next1[0]=-1; I=0; while(i<m-1)    { while(-1!=j && X[i]!=x[j]) j=next1[j];    Next1[++i]=++j; }/// at Next[m] This point has to be "mismatch".    ///In this way, all prefixes with the same suffix can be obtained.     while(-1!=J) {if(X[i]==x[j]) ans[cnt++]=j+1;    J=NEXT1[J]; }}intMain () {Freopen ("In.txt","R", stdin); while(scanf('%s ', str)!=eof) {intlen=strlen(str); Cnt=0; Kmp_next1 (STR,LEN,NEXT1); for(inti=cnt-1; i>=0; i--)printf("%d", Ans[i]);printf("%d\n", Len); }return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 2752+kmp+ uses next array properties to find all the same prefixes and suffixes

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.