1860. BOB or BBO
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Definition 1:If string S=(S0S1S2…Sn),Ror(S)=(SnS0S1…Sn-1)
Definition 2:Ror0(S)=S,Rorn(S)=Ror(Rorn-1(S))
Definition 3:Pow(S)={Ror0(S),Ror1(S),…,Rorlen(S)-1(S)}
Now your task is to find the string T which has the smallest lexicographic order in Pow(S).
For example,S=BOB,Ror0(S)=BOB,Ror1(S)=BBO,Ror2(S)=OBB,Pow(S)={BOB,BBO,OBB}.
Obviously,T=BBO is the correct answer.
Input
The first line of the input is a positive integer.It is the number of the test cases followed. Each test case contains a string S,which consists of capital letters.The length of string S isn’t larger than 10000.
Output
The output of the program should consist of one line of output for each test case. The output of each test case only contains the string T. No any redundant spaces are needed.
Sample Input
1 BOB
Sample Output
BBO
// Problem#: 1860
// Submission#: 3343303
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <algorithm>
#include <iostream>
#include <string>
#include <stdio.h>
#include <queue>
#include <string.h>
#include <vector>
#include <iomanip>
#include <map>
#include <stack>
#include <functional>
#include <list>
#include <cmath>
using namespace std;
int main() {
std::ios::sync_with_stdio(false);
int caseNum;
cin >> caseNum;
while (caseNum--) {
string s;
cin >> s;
int l = s.size();
char minC = 'Z';
for (int i = 0; i < l; i++) {
if (minC > s[i]) {
minC = s[i];
if (minC == 'A') break;
}
}
list<vector<int> > v;
for (int i = 0; i < l; i++) {
if (s[i] == minC) {
v.push_back(vector<int>(1, i));
}
}
int counter = 1;
while (v.size() > 1 && counter < l && counter + v.size() <= l) {
char minC = 'Z';
for (list<vector<int> >::iterator iter = v.begin(); iter != v.end(); iter++) {
int last = (*iter)[iter->size() - 1];
int next = last + 1;
if (next == l) next = 0;
if (s[next] < minC) {
minC = s[next];
if (minC == 'A') break;
}
}
for (list<vector<int> >::iterator iter = v.begin(); iter != v.end(); ) {
int last = (*iter)[iter->size() - 1];
int next = last + 1;
if (next == l) next = 0;
if (s[next] != minC) {
list<vector<int> >::iterator del = iter;
iter++;
v.erase(del);
} else {
iter->push_back(next);
iter++;
}
}
counter++;
}
string ans;
int pos = (*(v.begin()))[0];
for (int i = 0; i < l; i++) {
ans.push_back(s[pos]);
pos = pos + 1;
if (pos == l) pos = 0;
}
cout << ans << endl;
}
//getchar();
//getchar();
return 0;
}