Algorithm Practice for 1583

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. 

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
,.;\'`\"()/:-
.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.

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());
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值