/**============================================================================= * ACSL Intermediate Division Contest#2 2007-2008 Programming Problem: Data Compression * * Programmed and documented by: Anna Yu, Sir Winston Churchill High School * ============================================================================= */ import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.*; public class Anna { /** * @param args */ public static void main(String[] args) throws IOException { BufferedReader in = new BufferedReader( new FileReader("InterTest.in")); for (int i = 0; i < 5; i++) //loops 5 times for the 5 lines of input { StringTokenizer st = null; st = new StringTokenizer(in.readLine()); //creates 4 String arrays. one for words, one for the corresponding number of each word. //one for punctuations, one for the corresponding number of each punctuation ArrayList words = new ArrayList(); ArrayList count = new ArrayList(); ArrayList punc = new ArrayList(); ArrayList pCount = new ArrayList(); int k = st.countTokens();// loops until each token in the line has been processed for (int j = 0; j < k; j++) { String temp = st.nextToken(); String temp2 = ""; //initiates a temp2 for possible punctutations if (temp.endsWith("'") || temp.endsWith(":") || temp.endsWith(",") || temp.endsWith("!") || temp.endsWith(".") || temp.endsWith("?") || temp.endsWith("/") || temp.endsWith(";")) // cuts off any punctuations that may follow a word (e.g. YOU? into YOU as temp, and ? as temp2) { temp2 = temp.substring(temp.length() - 1, temp.length()); temp = temp.substring(0, temp.length() - 1); } if (temp.endsWith("-"))//a special case of punctuation that's separated from the word before it with a space, treated differently from punctuations mentioned above temp2 = temp; else { if (words.indexOf(temp) == -1)//tests whether the word already exists in the array { words.add(temp);//adds the word into the array if it's not found count.add(words.indexOf(temp), "a"); // the character a is used to count the number of times a words occurs, //because the string class requires no wrapping. //adds an a into the corresponding index in the count Array } else count.set(words.indexOf(temp), count.get(words .indexOf(temp))+ "a"); //if the word already exists, it adds an a to the corresponding index in the count array } if (temp2 != "") //only goes into this loop if the temp2 has been changed i.e. there was a punctuation { if (punc.indexOf(temp2) == -1) { punc.add(temp2); //same as above but for the punctuations pCount.add(punc.indexOf(temp2), "a"); } else pCount.set(punc.indexOf(temp2), pCount.get(punc .indexOf(temp2))+ "a"); } } //prints out the number of times a word occured //by finding how the number of "a"s in the count array of that particular index // and the word //then the same thing but for the punctuations for (int j = 0; j < words.size(); j++) System.out.print(count.get(j).length() + words.get(j)); for (int j = 0; j < punc.size(); j++) System.out.print(pCount.get(j).length() + punc.get(j)); System.out.println(""); } } }