Lang:Java
Edit12345678910111213141516171819202122232425262728293031import java.util.Scanner;public class Main {public static void main(String[] args) throws Exception {Scanner in = new Scanner(System.in);char[] queue = in.next().toCharArray();in.close();if (queue.length == 1) { // 长度为1System.out.println(queue[0]);return;}int[] l = new int[26];for (int i = 0; i < queue.length; ++i) {l[queue[i] - 'a']++;}for (int i = 0; i < l.length; ++i) {if (2 * l[i] > queue.length + 1) {System.out.println("INVALID");return;}}StringBuffer result = new StringBuffer();int[] top = getTop2(l);while (true) {char ca = (char) (top[0] + 'a');char cb = (char) (top[1] + 'a');while (l[top[0]] != 0 && l[top[1]] != 0) {result.append(ca + "" + cb);l[top[0]]--;l[top[1]]--;