字符串最小表示法
见最小表示法 YY的云笔记
import java.util.Scanner;
public class Main {
public static String mini(String s) {
s = s + s;
boolean flag = false;
int i = 0, j = 1, k, l = s.length() / 2, p = 0;
while (i < l && j < l) {
k = 0;
while (s.charAt(i + k) == s.charAt(j + k) && k < l) k++;
if (k == l) {
p = i;
flag = true;
break;
}
if (s.charAt(i + k) > s.charAt(j + k))
if (i + k + 1 > j)
i = i + k + 1;
else i = j + 1;
else
if (j + k + 1 > i) j = j + k + 1; else j = i + 1;
}
if (!flag)
if (i < j) p = i; else p = j;
return s.substring(p, p + l);
}
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
int t = cin.nextInt();
for (int i = 0; i < t; i++) {
String s = cin.next();
System.out.println(mini(s));
}
}
}