Choose Your Words Carefully
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 1707 | Accepted: 513 |
Description
Let's face it, most students who do a good job on programming contest problems aren't the best writers. However, there is no artistic process that a good programmer can't improve with a little program to automate the process.
In this case, your writing problem is a tendency to use words too often. To help check for this,you are going to write a program that will search your papers for the word or words you use most often.
In this case, your writing problem is a tendency to use words too often. To help check for this,you are going to write a program that will search your papers for the word or words you use most often.
Input
The input consists entirely of your paper. Each line will be under 80 characters and will contain words, punctuation, and numbers. Words consist of the characters {a-z,A-Z,0-9}. Words are separated by whitespace, end-of-line, and punctuation. The punctuation that may be found includes the characters
The input ends at end-of-file.
Word comparisons are case-insensitive.
,.;\'`\"()/:-.No other characters will be found in the input.
The input ends at end-of-file.
Word comparisons are case-insensitive.
Output
Your output begins with the line:
< n > occurrences
where n is the number of times the most frequently appearing word occurs.
Following that line will be the words that occurred n times, one per line. The words must be listed in alphabetic order.
< n > occurrences
where n is the number of times the most frequently appearing word occurs.
Following that line will be the words that occurred n times, one per line. The words must be listed in alphabetic order.
Sample Input
Fourscore and seven years ago our fathers brought forth on this continent a new nation, conceived in liberty and dedicated to the proposition that all men are created equal. Now we are engaged in a great civil war, testing whether that nation or any nation so conceived and so dedicated can long endure.
Sample Output
3 occurrences and nation
Solution
package id1583;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Hashtable;
import java.util.LinkedList;
public class ChooseYourWordsCarefully {
Hashtable<String,Integer> wordsTable = null;
BufferedReader in = null;
public ChooseYourWordsCarefully(){
wordsTable = new Hashtable<String,Integer>();
in = new BufferedReader(new InputStreamReader(System.in));
}
public static void main(String[] args) {
try {
new ChooseYourWordsCarefully().start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void start() throws IOException{
String buffer = null;
String[] words = null;
String str = "";
int count = 0;
int maxTime = 0;
LinkedList<String> maxTimeWordList = null;
char[] punctuations = new char[]{',', '.', ';', '\\', '\'', '`', '"', '(', ')', '/', ':', '-'};
str = in.readLine();
while(str != null && !in.equals("")){
buffer += " " + str;
str = in.readLine();
maxTimeWordList = new LinkedList<String>();
}
for(int i = 0; i < punctuations.length; i++)
buffer = buffer.replace(punctuations[i], ' ');
words = buffer.split((" +"));
for(int i = 0; i < words.length; i++)
if(wordsTable.containsKey(words[i])) {
count = wordsTable.get(words[i]) + 1;
if(count > maxTime) {
maxTime = count;
maxTimeWordList = new LinkedList<String>();
maxTimeWordList.offer(words[i]);
}
else if(count == maxTime)
maxTimeWordList.offer(words[i]);
wordsTable.put(words[i], count);
} else {
wordsTable.put(words[i],1);
}
System.out.println(maxTime + " occurrences");
while(!maxTimeWordList.isEmpty())
System.out.println(maxTimeWordList.poll());
}
}