标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2000 Accepted Submission(s): 875
1 #include<iostream> 2 #include<cstring> 3 #include<cstdio> 4 #include<algorithm> 5 6 using namespace std; 7 8 const int maxn = 1e6+7; 9 10 char s[maxn*2], str[maxn]; 11 int Next[maxn], len; 12 13 void Getnext(char s[]) 14 { 15 int j, k; 16 j = 0; 17 k = Next[0] = -1; 18 while(j < len) 19 { 20 while(k != -1 && s[j] != s[k]) 21 k = Next[k]; 22 Next[++j] = ++k; 23 } 24 } 25 26 int Getmax(char s[]) // 最大最小表示法 27 { 28 int i, j; 29 i = 0; 30 j = 1; 31 while(i < len && j < len) 32 { 33 int k = 0; 34 while(s[i+k] == s[j+k] && k < len) 35 k++; 36 if(k == len) 37 break; 38 39 if(s[i+k] > s[j+k]) 40 { 41 if(j+k > i) 42 j = j+k+1; 43 else 44 j = i + 1; 45 } 46 else 47 { 48 if(i+k > j) 49 i = i + k + 1; 50 else 51 i = j + 1; 52 } 53 } 54 return min(i, j); 55 } 56 57 int Getmin(char s[]) 58 { 59 int i, j; 60 i = 0; 61 j = 1; 62 while(i < len && j < len) 63 { 64 int k = 0; 65 while(s[i+k] == s[j+k] && k < len) 66 k++; 67 if(k == len) 68 break; 69 if(s[i+k] < s[j+k]) 70 { 71 if(j+k > i) 72 j = j+k+1; 73 else 74 j = i + 1; 75 } 76 else 77 { 78 if(i+k > j) 79 i = i + k + 1; 80 else 81 i = j + 1; 82 } 83 } 84 return min(i, j); 85 } 86 87 int main() 88 { 89 while(gets(str)) 90 { 91 len = strlen(str); 92 strcpy(s, str); 93 strcat(s, str); 94 95 memset(Next, 0, sizeof(Next)); 96 97 Getnext(str); 98 int t = 1, k = len - Next[len]; // k 最小循环节的长度,最小循环节,神奇的东西…… 99 100 if(len % k == 0) 101 t = len / k; 102 printf("%d %d %d %d\n", Getmin(s)+1, t, Getmax(s)+1, t); 103 } 104 return 0; 105 }
写代码你就好好写代码………………………………………………………………………………………………
标签:
原文地址:http://www.cnblogs.com/Tinamei/p/4817511.html