import java.io.*; import java.util.StringTokenizer; class Person { String orig; String name; int numMoves; Person(String orig, String name, int init) { this.orig = orig; this.name = name; this.numMoves = init; } } public class probb { BufferedReader br; BufferedWriter bw; int numDataSets; int currentDataSet; Person[] peopleList; int currentPeopleCount = 0; public probb() { try { br = new BufferedReader(new FileReader("probb.in")); bw = new BufferedWriter(new FileWriter("probb.out")); String str = br.readLine(); numDataSets = Integer.parseInt(str); currentDataSet = 1; } catch (Exception e) { e.printStackTrace(); } } public void process() { try { String str = br.readLine(); int numPeople = Integer.parseInt(str); currentPeopleCount = 0; peopleList = new Person[numPeople]; for (int i = 0; i < numPeople; i++) { String orig = br.readLine(); String person = orig.trim().toUpperCase(); StringTokenizer st = new StringTokenizer(person," "); String personName = ""; int count = st.countTokens(); for (int j = 0; j < count; j++) { personName += st.nextToken(); } Person p = new Person(orig,personName,0); insert(p); } int max = findMax(); String solString = "Data Set "+currentDataSet+":"; bw.write(solString,0,solString.length()); bw.newLine(); solString = "The following have to move "+max+" times:"; bw.write(solString,0,solString.length()); bw.newLine(); for (int k = 0; k < currentPeopleCount; k++) { outMax(k,max); } if (currentDataSet < numDataSets) bw.newLine(); currentDataSet++; } catch (Exception e) { e.printStackTrace(); } } public void outMax(int num, int max) { try { if (peopleList[num].numMoves == max) { String name = peopleList[num].orig; bw.write(name,0,name.length()); bw.newLine(); } } catch (Exception e) { e.printStackTrace(); } } public void insert(Person p) { if (currentPeopleCount == 0) { currentPeopleCount++; peopleList[0] = p; return; } int index = 0; boolean flag = false; while (!flag) { if (peopleList[index] == null) { flag = true; } else if (p.name.compareTo(peopleList[index].name) > 0){ index++; } else flag = true; } for (int k = currentPeopleCount; k > index; k--) { peopleList[k] = peopleList[k-1]; peopleList[k].numMoves++; } peopleList[index] = p; currentPeopleCount++; } public int findMax() { int currentMax = 0; for (int i = 0; i < currentPeopleCount; i++) if (peopleList[i].numMoves > currentMax) currentMax = peopleList[i].numMoves; return currentMax; } public void execute() { for (int i = 0; i < numDataSets; i++) { process(); } try { bw.close(); } catch (Exception e) { e.printStackTrace(); } } public static void main(String[] args) { probb p = new probb(); p.execute(); } }