String Problem---hdu3374 (KMP, dictionary order max and Min)

Source: Internet
Author: User

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=3374

test instructions is very simple, the output is The minimum number of dictionary order, the minimum number of dictionaries, the maximum number of dictionaries, and the maximum number of dictionary order.

You can think about it because it is a circular movement, so the maximum number of dictionary order and the number of dictionary order equals = follow-up;

Subject There is a maximum minimum representation:

Here is a brief introduction to the minimum representation of a string:

(1) using two pointers P1, p2. When initialized P1 points to s[0], P2 points to s[1].

(2) k = 0 start, test s[p1+k] and s[p2+k] corresponding characters are equal, if the equality is k++, until the first difference is found, (if K tried the length of a string has not found a difference, then that position is the smallest representation position, the algorithm terminates and returns).

In this process, s[p1+k] and s[p2+k] the size of the relationship, there are three kinds of situations:

(A). S[p1+k] > S[p2+k], the P1 slide to p1+k+1---that is s1[p1->p1+k] will not be prefixed with the "minimum representation" of the loop string. K-Position 0

(B). S[p1+k] < s[p2+k], p2 slide to p2+k+1, K to 0

(C). S[p1+k] = s[p2+k], then k++; if (k = = len) returns the result.

Note: There is a small detail of the sliding method here, if p1 = = P2 after sliding, the pointer is changing +1. Until P1, p2 the entire string is checked, returns the value of both small to Len.

(3) If k = = Len, returns the minimum value in P1 and P2

The maximum and minimum representations are exactly the same, and a simple change can be done.

This method takes two pointers, representing the beginning of the two strings, if the beginning is different directly to the dictionary order large back shift, if the beginning of the same then use a count length k to move around, know found s[i+k]! = S[j+k] When

If K==n then these two strings are the smallest dictionary order, otherwise, let the value of the pointer move backward until there is a pointer over N.

Code

#include <stdio.h>#include<string.h>#include<algorithm>#include<stdlib.h>using namespacestd;Const intN = 2e6+7;CharS[n], s0[n];intNext[n];voidGetNext (CharA[],intN) {    intI=0, j=-1; next[0] = -1;  while(i<N) {if(j==-1|| a[i]==A[j]) next[++i] = + +J; ElseJ=Next[j]; }}intMin_index (CharA[],intN) {    intI=0, j=1, k=0;  while(I<n && j<n && k<N) {intt = a[(i+k)%n]-a[(j+k)%N]; if(T = =0) K++; Else        {            if(T <0) J= j + k +1; ElseI= i + K +1; if(i = =j) J++; K=0; }    }    returnmin (i, j);}intMax_index (CharA[],intN) {    intI=0, j=1, k=0;  while(I<n && j<n && k<N) {intt = a[(i+k)%n]-a[(j+k)%N]; if(T = =0) K++; Else        {            if(T <0) I= i + K +1; ElseJ= j + k +1; if(i = =j) J++; K=0; }    }    returnmin (i, j);}intMain () {intLen, cycle, ans, Min, Max;  while(SCANF ("%s", S0)! =EOF) {Len=strlen (S0);        GetNext (S0, Len); Cycle= Len-Next[len]; if(len%cycle==0) ans= len/cycle; Elseans=1;        strcpy (S, S0);        Strcat (S, S0); Min= Min_index (s, len) +1; Max= Max_index (s, len) +1; printf ("%d %d%d%d\n", Min, ans, Max, ans); }    return 0;}
View Code

String Problem---hdu3374 (KMP, dictionary order max and Min)

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.