HDOJ3374 String Problem "KMP" + "minimal notation"

Source: Internet
Author: User

String problemTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 1512 Accepted Submission (s): 668

Problem descriptiongive you a string with the length n, you can generate n strings by the left shifts. For example let consider the string "Skylong", we can generate seven strings:
String Rank
Skylong 1
Kylongs 2
YLONGSK 3
Longsky 4
Ongskyl 5
Ngskylo 6
Gskylon 7
and lexicographically first of them is Gskylon, lexicographically last was Ylongsk, both of them appear only once.
Your task is easy, calculate the lexicographically fisrt string ' s Rank (if there is multiple answers, choose the smallest One), its times, lexicographically last string ' s Rank (if there is multiple answers, choose the smallest one), and its T IMEs also.

Input each line contains one line the string S with a length n (n <= 1000000) formed by lower case letters.
Outputoutput four integers separated by one space, lexicographically fisrt string ' s Rank (if there is multiple answers, c Hoose the smallest one), the string ' s times in the N generated strings, lexicographically last string ' s Rank (if there is Multiple answers, choose the smallest one), and its times also.
Sample Input
Abcderaaaaaaababab

Sample Output
1 1 6 11 6 1 61 3 2 3
I did it once before. Today, when doing the minimum notation to forget, with ordinary enumeration to find the smallest dictionary order, decisive timeout.

Test Instructions: given a string. The original string sequence number is 1, each left one time ordinal plus one, to find the smallest dictionary ordinal and the maximum number of dictionary sequence and number of cycles. Solving the problem: the number of cycles only need to find the loop node, the difficulty is to find the smallest (large) dictionary sequence ordinal, to use the minimum notation, this is used as a template. Where the minimum representation is difficult to understand in s[i+k]>s[j+k] I need to +=k+1, this is because s[i...i+k-1] and s[j...j+k-1] are equal. Note that any string starting with S[i...i+k] will be greater than the string starting with S[j].

#include <stdio.h> #define MAXN 1000002char str[maxn];int NEXT[MAXN], Len, Cir, Minpos, maxpos;void getNext () {int    i = 0, j =-1;    Next[0] =-1;            while (Str[i]) {if (j = =-1 | | str[i] = = Str[j]) {++i; ++j; Next[i] = j;    Mode 1}else j = next[j]; } len = i;}    void Findminandmaxpos () {minpos = Maxpos = 0;    int k = 0, pos = 1, t;  while (Minpos < Len && k < len && Pos < len) {t = str[(Minpos + k)% len]-str[(pos + k)%        Len];        if (t = = 0) {++k; continue;}        else if (T < 0) pos + = k + 1;        else Minpos + = k + 1;        k = 0;    if (Minpos = = pos) ++pos;    } if (Minpos > pos) minpos = pos;    ++minpos; k = 0;    pos = 1;  while (Maxpos < Len && k < len && Pos < len) {t = str[(Maxpos + k)% len]-str[(pos + k)%        Len];        if (t = = 0) {++k; continue;}        else if (T > 0) pos + = k + 1;        else Maxpos + = k + 1;        k = 0; If(Maxpos = = pos) ++pos;    } if (Maxpos > pos) maxpos = pos; ++maxpos;}    int main () {//freopen ("Stdin.txt", "R", stdin);         while (scanf ("%s", str)! = EOF) {getNext (); Findminandmaxpos ();        CIR = 1;        if (len% (Len-next[len]) = = 0) Cir = len/(Len-next[len]);    printf ("%d%d%d%d\n", Minpos, Cir, maxpos, CIR); } return 0;}



HDOJ3374 String Problem "KMP" + "minimal notation"

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.