Algorithm Practice for 1583
来源:互联网 发布:淘宝为什么不能付款 编辑:程序博客网 时间:2024/05/18 03:40
Choose Your Words Carefully
Time Limit: 1000MS Memory Limit: 10000KTotal 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 thiscontinent a new nation, conceived in liberty and dedicated to theproposition that all men are created equal. Now we are engaged ina great civil war, testing whether that nation or any nation soconceived and so dedicated can long endure.
Sample Output
3 occurrencesandnation
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 blocke.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());}}
- Algorithm Practice for 1583
- Algorithm Practice for 1579
- Algorithm Practice for 2002
- Algorithm Practice for 1578
- Algorithm Practice for 1580
- Algorithm Practice for 1581
- Algorithm Practice for 1582
- Algorithm Practice for 1577
- Algorithm Practice for 1576
- Algorithm Practice for 1575
- Algorithm Practice for 1574
- Algorithm Practice for 1548
- PAT Algorithm Practice
- Genetic Algorithm for VRP
- Links for Algorithm
- Algorithm for creating spheres?
- Best Practice for SQL Server
- GUN tar for windows Practice
- Android--MediaPlayer音乐播放器歌词随时间高亮显示
- Loading and starting DSP server c6accel_omap3530.x64P FAILED
- android2.3 dvsdk .
- TI OMAP MUX configure .
- 学习TI DVSDK过程记录(一):视频数据从哪里来到哪里去 2012-08-18 15:01:14
- Algorithm Practice for 1583
- OMAP3530 目标板执行video_copy 例程出错 求帮助?
- change Memory map for 256M
- [LeetCode] 由前序和中序序列,构建二叉树
- 从前序遍历序列恢复BST
- LeetCode24:Reverse Nodes in k-Group
- Sequence Alignment
- LeetCode25:Remove Duplicates from Sorted Array
- C++实现栈机制