War+Game

A boy named Gil-Dong is playing Computer Strategy game these days. To win this game, he has to find out opponent’s formation and then, he must organize his formation with appropriate soldiers. In this game there are three sorts of soldiers, which are archer, lancer, and horse rider units. An archer unit can beat a lancer unit, and a lancer unit can beat a horse rider unit, and a horse rider unit can beat an archer unit. Your task is to make a program providing Gil-Dong a winning formation of the game with the minimum number of soldiers.

Important Notes: 1. Every soldier can only attack forward, and can’t attack the side. 2. Gil-Dong’s soldiers are moving from the right to the left, and the opponent’s soldiers are moving from the left to the right as the right figure shown. 3. When a soldier of Gil-Dong fight with the same kind soldier of the opponent, Gil-Dong’s soldier should be beaten because Gil-Dong’s soldiers are not really well trained.

Input
Your program is to read from standard input. The input consists of T (1≤T≤15) test cases. The number T of test cases is given in the first line of the input. Each test case consists only of one formation of soldiers represented by a string of {A, B, C} in each following line, where ‘A’ denotes a archer unit, ‘B’ denotes a lancer unit, ‘C’ denotes a horse rider unit, respectively. The length of each test case does not exceed 80. You shoud remember that the last soldier of the given formation is involved in the first engagement with the first soldier of Gil-Dong’s formation.

Output
Your program is to write to standard output. Print exactly one line for each test case with a winning formation of the minimum possible length. The following shows sample input and output for three test cases.

Sample Input
3 ABCBCACBA BCCAABCA ACAABBCA

Sample Output
CABCBABAC CBACBA CBACBC

Java
code format="java5" public class WarGame { public static void main(String[] args) { //char[] enemy = {'A', 'B', 'C', 'B', 'C', 'A', 'C', 'B', 'A'}; char[] enemy = {'B', 'C', 'C', 'A', 'A', 'B', 'C', 'A'};

win(enemy); }

// 보다 강한 유닛을 반환한다. public static void convert(char[] enemy) { for(int i = 0; i < enemy.length; i++) { switch(enemy[i]) { case 'A': enemy[i] = 'C'; break; case 'B': enemy[i] = 'A'; break; case 'C': enemy[i] = 'B'; break; }       }    }

// 연속된 유닛은 삭제한다. public static void remove(char[] enemy) { char pre = enemy[0];

for(int i = 1; i < enemy.length; i++) { if(pre == enemy[i]) { enemy[i] = 'X'; }           pre = enemy[i]; }   }

// 유닛들의 순서를 뒤집는다. public static void reverse(char[] enemy) { char[] newEnemy = new char[enemy.length];

for(int i = 0; i < enemy.length; i++) { newEnemy[i] = enemy[enemy.length - i - 1]; }

for(int i = 0; i < enemy.length; i++) { enemy[i] = newEnemy[i]; }   }

// 유닛들을 출력한다. public static void print(char[] enemy) { for(int i = 0; i < enemy.length; i++) { if(enemy[i] != 'X') { System.out.print(enemy[i]); }       }        System.out.println; }

public static void win(char[] enemy) { remove(enemy); convert(enemy); reverse(enemy); print(enemy); } } code