HDU 5442 Favorite Donut
Favorite Donut
Time Limit: 1500/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 79 Accepted Submission(s): 17
Lulu 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 of n parts. Every part has its own sugariness that can be expressed by a letter from a to z (from low to high), and a ring donut can be expressed by a string whose i-th character represents the sugariness of the i−th part in clockwise order. Note that z is the sweetest, and two parts are equally sweet if they have the same sugariness.
Once Lulu eats a part of the donut, she must continue to eat its uneaten adjacent part until all parts are eaten. Therefore, she has to eat either clockwise or counter-clockwise after her first bite, and there are 2n ways to eat the ring donut of n parts. For example, Lulu has 6 ways to eat a ring donut abc: abc,bca,cab,acb,bac,cba. Lulu likes eating the sweetest part first, so she actually prefer the way of the greatest lexicographic order. If there are two or more lexicographic maxima, then she will prefer the way whose starting part has the minimum index in clockwise order. If two ways start at the same part, then she will prefer eating the donut in clockwise order. Please compute the way to eat the donut she likes most.
Input
First line contain one integer T,T≤20, 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 has. The next line contains a string consisted of n lowercase alphabets representing the ring donut.
Output
You should print one line for each test case, consisted of two integers, which represents the starting point (from 1 to n) and the direction (0 for clockwise and 1 for counterclockwise).
解题:后缀自动机
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int maxn = 100010; 4 struct SAM { 5 struct node { 6 int son[26],f,len; 7 void init() { 8 f = -1; 9 len = 0; 10 memset(son,-1,sizeof son); 11 } 12 } sn[maxn<<1]; 13 int tot,last; 14 void init() { 15 tot = last = 0; 16 sn[tot++].init(); 17 } 18 int newnode() { 19 sn[tot].init(); 20 return tot++; 21 } 22 void extend(int c) { 23 int np = newnode(),p = last; 24 sn[np].len = sn[last].len + 1; 25 while(p != -1 && sn[p].son[c] == -1) { 26 sn[p].son[c] = np; 27 p = sn[p].f; 28 } 29 if(p == -1) sn[np].f = 0; 30 else { 31 int q = sn[p].son[c]; 32 if(sn[p].len + 1 == sn[q].len) sn[np].f = q; 33 else { 34 int nq = newnode(); 35 sn[nq] = sn[q]; 36 sn[nq].len = sn[p].len + 1; 37 sn[np].f = sn[q].f = nq; 38 while(p != -1 && sn[p].son[c] == q) { 39 sn[p].son[c] = nq; 40 p = sn[p].f; 41 } 42 } 43 } 44 last = np; 45 } 46 } sam; 47 char str[maxn]; 48 int fail[maxn]; 49 void getFail(const char *word) { 50 fail[0] = -1; 51 fail[1] = 0; 52 for(int i = 0,j = -1; word[i]; ++i) { 53 while(j != -1 && word[i] != word[j]) j = fail[j]; 54 fail[i+1] = ++j; 55 } 56 } 57 char text[maxn]; 58 int mymin(int x,int y) { 59 return x > y?y:x; 60 } 61 int mymax(int x,int y) { 62 return x > y?x:y; 63 } 64 int FindIndex(const char *word,int (*f)(int,int),int idx,int len) { 65 for(int i = 0, j = 0; text[i] ; ++i) { 66 while(j > -1 && word[j] != text[i]) j = fail[j]; 67 if(!word[++j]) { 68 if(i - len + 2 <= len)idx = f(idx,i - len + 2); 69 j = fail[j]; 70 } 71 } 72 return idx; 73 } 74 int main() { 75 int kase,len; 76 scanf("%d",&kase); 77 while(kase--) { 78 sam.init(); 79 scanf("%d",&len); 80 scanf("%s",str); 81 int p = 0; 82 for(int i = 0; i < (len<<1); ++i) 83 sam.extend(str[i%len]-'a'); 84 string ss = "",sb = ""; 85 for(int i = 0; i < len; ++i) { 86 for(int j = 25; j >= 0; --j) { 87 if(sam.sn[p].son[j] != -1) { 88 p = sam.sn[p].son[j]; 89 ss += char('a' + j); 90 break; 91 } 92 } 93 } 94 int clockwise = sam.sn[p].len - len + 1; 95 getFail(ss.c_str()); 96 strcpy(text,str); 97 strcpy(text + len,str); 98 clockwise = FindIndex(ss.c_str(),mymin,len*2,len); 99 reverse(str,str + len); 100 sam.init(); 101 for(int i = p = 0; i < (len<<1); ++i) { 102 sam.extend(str[i%len]-'a'); 103 } 104 for(int i = 0; i < len; ++i) { 105 for(int j = 25; j >= 0; --j) { 106 if(sam.sn[p].son[j] != -1) { 107 p = sam.sn[p].son[j]; 108 sb += char('a' + j); 109 break; 110 } 111 } 112 } 113 getFail(sb.c_str()); 114 strcpy(text,str); 115 strcpy(text + len,str); 116 int anticlockwise = len - FindIndex(sb.c_str(),mymax,0,len) + 1; 117 if(ss == sb) { 118 if(clockwise == anticlockwise) { 119 printf("%d %d\n",clockwise,0); 120 } else if(clockwise < anticlockwise) { 121 printf("%d %d\n",clockwise,0); 122 } else printf("%d %d\n",anticlockwise,1); 123 } else if(ss < sb) printf("%d %d\n",anticlockwise,1); 124 else printf("%d %d\n",clockwise,0); 125 } 126 return 0; 127 }