UVa1584

題目:
https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=448&page=show_problem&problem=4459

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <bits/stdc++.h>

using namespace std;

int main(){
ios::sync_with_stdio(0);
cin.tie(0);

int n;
cin >> n >> ws;
while(n--){
string text;
getline(cin, text);
string mini = text; // 用mini紀錄最小的sequence
text.append(text); // 讓text尾端重複一遍,形成circular

int half = text.length()/2;
for(int i=1; i<half; ++i){ // 跑一整輪找最小的sequence
mini = min(mini, text.substr(i, half));
}

cout << mini << '\n';
}

return 0;
}