HDU 3374 String Problem (min max notation + KMP)

Source: Internet
Author: User

Question:
Given a string S, you can shift to the left to get another string. For example, S = "SKYLONG", all strings obtained by displacement (the following number indicates rank, that is, the number of digits ):
SKYLONG 1
KYLONGS 2
YLONGSK 3
LONGSKY 4
ONGSKYL 5
NGSKYLO 6
GSKYLON 7
Find the minimum Lexicographic Order and rank required by the dictionary and the number of occurrences of all strings after a string is displaced.

Analysis and Summary:
After analysis, we can quickly get a rough idea:
1. Copy S to 2 times
2. Find the minimum and maximum lexicographic strings
3. directly use KMP to find the number of times.
The key lies in step 2. During the first operation, the data is enumerated directly and the result is TLE. It is necessary to find the smallest and largest lexicographic strings in a more efficient way.
Baidu learned that this method is the "minimum maximum notation ".
MATERIALS:
[Application of the "minimal expression" concept in the character string loop homogeneous problem -- 03 Zhou yuan]: 1. Paper 2.ppt


Code:
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <algorithm>
Using namespace std;
 
Const int MAXN = 1000005;
Char S [MAXN * 2];
Char first [MAXN];
Char last [MAXN];
Int rank_first, rank_last;
Int next [MAXN];
 
Void getNext (char * S, int * next ){
Int n = strlen (S );
Next [0] = next [1] = 0;
For (int I = 1; I <n; ++ I ){
Int j = next [I];
If (j & S [I]! = S [j]) j = next [j];
Next [I + 1] = S [I] = S [j]? 1 + j: 0;
}
}
 
Int find (char * S, char * P, int * next ){
GetNext (P, next );
Int n = strlen (S );
Int m = strlen (P );
Int j = 0;
Int cnt = 0;
For (int I = 0; I <n; ++ I ){
While (j & S [I]! = P [j]) j = next [j];
If (S [I] = P [j]) ++ j;
If (j = m ){
++ Cnt;
}
}
Return cnt;
}
// Minimum representation
Void getMin (char * S ){
Int I = 0, j = 1;
Int len = strlen (S );
Len> = 1;
While (I <len & j <len ){
Int k = 0;
While (k <len & S [I + k] = S [j + k])
++ K;
If (k> = len)
Break;
If (S [I + k]> S [j + k])
I = max (I + k + 1, j + 1 );
Else
J = max (I + 1, j + k + 1 );
}
Int pos = min (I, j );
Rank_first = pos + 1;
For (int I = 0; I <len; ++ I)
First [I] = S [pos ++];
First [len] = '\ 0 ';
}
// Maximum representation
Void getMax (char * S ){
Int I = 0, j = 1;
Int len = strlen (S );
Len> = 1;
While (I <len & j <len ){
Int k = 0;
While (k <len & S [I + k] = S [j + k])
++ K;
If (k> = len)
Break;
If (S [I + k] <S [j + k])
I = max (I + k + 1, j + 1 );
Else
J = max (I + 1, j + k + 1 );
}
Int pos = min (I, j );
Rank_last = pos + 1;
For (int I = 0; I <len; ++ I)
Last [I] = S [pos ++];
Last [len] = '\ 0 ';
}


Int main (){
While (scanf ("% s", S )! = EOF ){
Int len = strlen (S );
For (int I = 0; I <len; ++ I ){
S [len + I] = S [I];
}
S [2 * len] = '\ 0 ';
// First, find the minimum and maximum ranking of the dictionary.
GetMin (S );
GetMax (S );
Printf ("% d \ n", rank_first, find (S + 1, first, next), rank_last, find (S + 1, last, next ));
}
Return 0;
}

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.