UVa1584 Circular Sequence
#include <stdio.h>
#include <string.h>
int less(char* str, size_t len, size_t p, size_t q)
{
size_t i, a, b;
for (i = 0; i < len; ++i)
{
a = (p+i) % len;
b = (q+i) % len;
if (str[a] < str[b])
return 1;
if (str[a] > str[b])
return 0;
}
return 0;
}
int main()
{
int T;
size_t i, ans, len;
char str[101];
scanf("%d", &T);
while (T--)
{
scanf("%s", str);
len = strlen(str);
ans = 0;
for (i = 1; i < len; ++i)
{
if (less(str, len, i, ans))
ans = i;
}
for (i = 0; i < len; ++i)
putchar(str[(i+ans)%len]);
putchar('\n');
}
return 0;
}