Problem:
English Sentence

Your task is to write a program which reads a text and prints two words. The first one is the word which is arise most frequently in the text. The second one is the word which has the maximum number of letters.

The text includes only alphabetical characters and spaces. A word is a sequence of letters which is separated by the spaces.

Input

A text is given in a line. You can assume the following conditions:

The number of letters in the text is less than or equal to 1000.

The number of letters in a word is less than or equal to 32.

There is only one word which is arise most frequently in given text.

There is only one word which has the maximum number of letters in given text.

Output

The two words separated by a space.

Sample Input

Thank you for your mail and your lectures

Output for the Sample Input

your lectures

Solution in JAVA (judge verdict: Accepted)


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Scanner;

class Main {

	/*
	 * https://onlinejudge.u-aizu.ac.jp/#/problems/0029
	 */

	public static void main(String[] args) throws IOException {
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		Scanner scanner = new Scanner(br.readLine());
		HashMap<String, Integer> map = new HashMap<>();
		while (scanner.hasNext()) {
			String word = scanner.next();
			Integer frequency = map.get(word);
			if(frequency==null) {
				map.put(word, 1);
			}else {
				map.put(word, frequency+1);
			}
		}
		scanner.close();

		String[] wordArray = map.keySet().toArray(new String[0]);
		String mostFreq = wordArray[0];
		for(int i=1; i<wordArray.length; i++) {
			int freq = map.get(wordArray[i]);
			if(freq > map.get(mostFreq)) {
				mostFreq = wordArray[i];
			}
		}
		String maxLength = wordArray[0];
		for(String word : wordArray) {
			if(word.length() > maxLength.length()) {
				maxLength = word;
			}
		}

		System.out.printf("%s %s\n", mostFreq, maxLength);
	}

}