Some beads are embedded in a bracelet, and each beads is carved with a lower case letter, as the follow figure shows:
If we read the letters in counter clockwise with different initial position, we may get different strings. The above example, we can read 6 strings:
acabdb
cabdba
abdbac
bdbaca
dbacab
bacabd
Sort these strings in lexical order, we obtain:
abdbac
acabdb
bacabd
bdbaca
cabdba
dbacab
What we need is the first string: abdbac.
输入格式
A string of lower case letters representing the letters carved on the bracelet (in counter clockwise order).
The length of the string is no more than 100.
输出格式
Output the first string of all the possible strings after sorting them in lexical order.
样例输入
acabdb kkisverystupid
样例输出
abdbac dkkisverystupi
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int Comp(const void*p1,const void*p2){
return strcmp((char*)p1,(char*)p2);
}
int main(){
char in[101];
while(scanf("%s",in)!= EOF){
int len = strlen(in);
char out[101][101];
for(int i=0; i<len; i++){
for(int j=0; j<len; j++){
out[i][j] = in[(j + i)%len];
}
out[i][len] = '\0';
}
qsort(out,len,sizeof(out[0]),Comp);
printf("%s\n",out[0]);
}
return 0;
}