HDU 5442--favorite Donut —————— "Maximum representation +KMP | Suffix array "

Source: Internet
Author: User

Favorite Donut

Time limit:1500/1000 MS (java/others) Memory limit:131072/131072 K (java/others)
Total submission (s): 1702 Accepted Submission (s): 430


Problem Descriptionlulu has a sweet tooth. Her favorite food is ring donut. Everyday she buys a ring donut from the same bakery. A Ring Donut is consists ofNParts. Every part have its own sugariness that can is expressed by a letter fromaToZAnd a ring donut can expressed by a string whose i-th character represents the sugariness of thei−th part in clockwise order. Note thatZis the sweetest, and both parts is equally sweet if they has the same sugariness.

Once Lulu eats a part of the donut, she must continue to eat it uneaten adjacent part until all parts is eaten. Therefore, she had to eat either clockwise or counter-clockwise after her first bite, and there is2N Ways to eat the ring donut ofNParts. For example, Lulu have6Ways to eat a ring donutabc :ABc, Bca ,c Ab ac b, Bac ,c Ba . Lulu likes eating the sweetest part first, so she actually prefer the the-the-the-greatest-lexicographic order. If there is, or more lexicographic maxima, then she'll prefer the-the-part whose starting part have the minimum index in C Lockwise order. If the ways start at the same part, then she'll prefer eating the donut in clockwise order. Please compute the Eat the donut she likes most.

Inputfirst line contain one integerT,t≤ , which means the number of test case.

For each test case, the first line contains one integer n,n≤20000, which represents how many parts the Ring donut have. The next line contains a string consisted of n lowercase alphabets representing the ring donut.

Outputyou should print one line for each test case, consisted of a integers, which represents the starting point (FROM&N Bsp1  to  n ) and the direction (0  for clockwise And < Span id= "Mathjax-element-17-frame" class= "Mathjax" >1  for Counterclockwise).

Sample Input24abab4aaab

Sample Output2 04 0

Source2015 ACM/ICPC Asia Regional Changchun Online title: Traverse the string clockwise or counterclockwise to get the maximum dictionary order. If the maximum dictionary order is unique, then the output gets the position of the maximum dictionary order and the Traversal order. If not unique, then the position is small priority, if the location is the same, then clockwise priority. The result is the output position and the traversal order. Clockwise is 0, counter-clockwise is 1. Spit Groove: Himself was playing dead. The data range itself is small, the topic is 2W, their own yy 1W, wrong to die WA. Problem-solving idea: for the smallest subscript that gets the maximum dictionary order clockwise, it can be obtained directly by the maximum representation method. For the smallest subscript counterclockwise, we first reverse the original string, and then use the maximum notation to get subscript, but the resulting subscript, is not the original string in reverse order to get the largest dictionary sequence of the smallest subscript, but the largest. So we will copy the inverted string in another empty string, and then the tail with a reversed string, the length of 2n string as a text string. Then we extract the string of the largest dictionary sequence in reverse order in the original string as a pattern string, then match with KMP, when the match needs to get the maximum match position (because it is the inverse string, so the maximum is the smallest in the original string). The first is to compare whether the strings obtained in the positive and reverse order are the same, and the order of the traversal is different, the same is the position size, the small or the clockwise priority.
#include <bits/stdc++.h>using namespace Std;const int Maxn=1e5+500;char str[maxn],sp[maxn],revs[maxn],rvsp[    Maxn];char tx[3*maxn];int f[maxn];int Get_max (char *s) {//MAX (min) notation obtains the maximum (minimum) dictionary ordinal start position of the positive sequence traversal.    int i=0,j=1,k=0;    int Len=strlen (s);        while (I<len&&j<len) {if (K==len) {break;            } if (s[(i+k)%len]<s[(j+k)%len]) {i=i+k+1>j? i+k+1:j+1;        k=0;            }else if (s[(i+k)%len]>s[(j+k)%len]) {j=j+k+1>i? j+k+1:i+1;        k=0;        }else{k++; }} return min (i,j);}    void get_s_p (char *sr,char *s,int st,int len) {//extract string for (int i=st,k=0;k<len;i++,k++) {Sr[i-st]=s[i%len]; } sr[len]= ' + ';}    void Rev (char *s,int len) {//invert for (int i=len-1;i>=0;i--) {revs[len-1-i]=s[i]; } revs[len]= ' + ';}    void Getfail (char *p,int *f) {int M=strlen (P);    f[0]=f[1]=0;    Int J;        for (int i=1;i<m;i++) {j=f[i]; while (J&&p[i]!=p[j]) j=F[J];    f[i+1]=p[i]==p[j]?j+1:0;    }}int KMP (char *t,char *p) {int N=strlen (T), M=strlen (P);    int j=0;    int ret=0;        for (int. i=0;i<n-1;i++) {while (J&&p[j]!=t[i]) j=f[j];        if (P[j]==t[i]) j + +;     if (j==m) {Ret=max (ret,i-m+1);        For the inverted string, you need to get the maximum position j=f[j]; }} return ret;}    int main () {int t, n;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        scanf ("%s", str);        Rev (Str,n);        int Ckw=get_max (str);        Get_s_p (Sp,str,ckw,n);        int Ctckw=get_max (revs);        Get_s_p (Rvsp,revs,ctckw,n);        strcpy (Tx,revs);        strcat (Tx,revs);        Getfail (RVSP,F);   int ANS=KMP (TX,RVSP);    The position of the Ans=n-1-ans in the inverted string;        Convert to position int d=strcmp (SP,RVSP) in the original string;        if (d>0) {printf ("%d 0\n", ckw+1);        }else if (d<0) {printf ("%d 1\n", ans+1);            }else{if (Ckw<=ans) {printf ("%d 0\n", ckw+1); }else{               printf ("%d 1\n", ans+1); }}} return 0;}

  

HDU 5442--favorite Donut —————— "Maximum representation +KMP | Suffix array "

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.