[Minimum string representation] HDU 3374

Source: Internet
Author: User

Http://acm.hdu.edu.cn/showproblem.php? PID = 1, 3374

It turns out that this is the smallest representation...

Method: Calculate the minimum expression and the maximum expression respectively to find the initial position, and then compare the brute force. Use KMP twice to find the maximum and minimum occurrences of the string.

# Define n 1000010 char STR [N], ss [N * 2], LIN [N]; int P [N]; int minrp (char * s, int L) {// minimum int I = 0, j = 1, K = 0, T; while (I <L & J <L & K <L) {T = s [(I + k)> = L? I + k-l: I + k]-s [(j + k)> = L? J + k-l: J + k]; If (! T) K ++; else {If (T> 0) I = I + k + 1; else J = J + k + 1; if (I = J) + + J; k = 0;} return I;} int maxrp (char * s, int L) {// maximum int I = 0, j = 1, k = 0, T; while (I <L & J <L & K <L) {T = s [(I + k)> = L? I + k-l: I + k]-s [(j + k)> = L? J + k-l: J + k]; If (! T) K ++; else {If (T <0) I = I + k + 1; else J = J + k + 1; if (I = J) + + J; k = 0;} return I;} void KMP () {int Len = strlen (Lin); int I, J, K; P [0] =-1; j =-1; for (I = 1; I <Len; I ++) {While (j> = 0 & lin [I]! = LiN [J + 1]) J = P [J]; If (LiN [I] = LiN [J + 1]) J ++; P [I] = J ;}} int KMP _ (char * s) {int Len = strlen (s); int n = strlen (Lin); int I, j =-1; int sum = 0; for (I = 0; I <Len; I ++) {While (j> = 0 & S [I]! = LiN [J + 1]) J = P [J]; If (s [I] = LiN [J + 1]) J ++; if (j = n-1 & I! = Len-1) {sum + +; j = P [J];} return sum;} int main () {While (scanf ("% s", STR )! = EOF) {int I, j, k; int Len = strlen (STR); int minr = minrp (STR, Len) + 1; int maxr = maxrp (STR, Len) + 1; strcpy (SS, STR); strcat (SS, STR); For (k = 0, I = minr-1; I <Len; I ++) {LiN [k ++] = STR [I];} for (I = 0; I <minr-1; I ++) {LiN [k ++] = STR [I];} LiN [k] = '\ 0'; KMP (); int mint = KMP _ (SS ); for (k = 0, I = maxr-1; I <Len; I ++) {LiN [k ++] = STR [I];} for (I = 0; I <maxr-1; I ++) {LiN [K + +] = STR [I];} LiN [k] = '\ 0'; KMP (); int maxt = KMP _ (SS); printf ("% d \ n", minr, mint, maxr, maxt);} 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.