uva 1584.Circular Sequence

题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4459

题目意思:给出一个字符串,求出该字符串的最小字典序

暴力求解即可,用 ans 保存最小字典序的第一个字符的下标。(好像这方法有点慢,测试的时候,第二个 test 出来的答案好像要等那么一段时间 = =, 3s 应该挺够的)

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 using namespace std;
 6 
 7 const int maxn = 100 + 5;
 8 char s[maxn];
 9 int len;
10 
11 int cmp(const char *s, int p, int q)
12 {
13     for (int i = 0; i < len; i++) {
14         if (s[(p+i)%len] != s[(q+i)%len]) {
15             return s[(p+i)%len] < s[(q+i)%len];
16         }
17     }
18     return 0;  // 刚好给出的字符串就是最小字典序
19 }
20 
21 int main()
22 {
23     #ifndef ONLINE_JUDGE
24         freopen("in.txt", "r", stdin);
25     #endif // ONLINE_JUDGE
26 
27     int T;
28     while (scanf("%d", &T) != EOF) {
29         while (T--)
30             char s[maxn];
31             scanf("%s", s);
32             len = strlen(s);
33             int ans = 0;
34 
35             for (int i = 1; i < len; i++) {
36                 if (cmp(s, i, ans)) {
37                     ans = i;
38                 }
39             }
40 
41             for (int i = 0; i < len; i++) {
42                 printf("%c", s[(ans+i)%len]);
43             }
44             printf("
");
45     }
46     return 0;
47 }

  

原文地址:https://www.cnblogs.com/windysai/p/5348554.html