环状序列(UVa1584)

  题目具体描述见:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=4459

C++11代码如下:

 1 #include<iostream>
 2 #include<string.h>
 3 #define maxn 103
 4 using namespace std;
 5 char s[maxn];
 6 //C++中注意避免使用less作为自定义函数,因为会和标准库中的less函数重名,或者使用不同的空间域来界定
 7 bool less_seq(const char* s, int p, int q) {  //用来比较以p和q作为起始位置表示的哪种字典序小
 8     int n = strlen(s);
 9     for (int i = 0; i < n; i++) 
10         if (s[(p + i) % n] != s[(q + i) % n])  //使用 %n 来实现序列的循环
11             return s[(p + i) % n] < s[(q + i) % n];
12     return false;
13 }
14 
15 int main() {
16     int n, T;
17     cin >> T;
18     while (T--) {
19         int min = 0;   //定义字典序最小的起始位置为min
20         cin >> s;
21         n = strlen(s);
22         for (int i = 0; i < n; i++) 
23             if (less_seq(s, i, min)) min = i;  //更新min
24         for (int i = 0; i < n; i++)
25             cout << s[(min + i) % n];  //循环打印
26         cout << endl;        
27     }
28     return 0;
29 }
posted on 2018-06-25 18:10  Pink.Pig  阅读(613)  评论(0编辑  收藏  举报