People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker's identity, which is useful when validating, for example, whether it's still the same person behind an online avatar.
Now given a paragraph of text sampled from someone's speech, can you find the person's most commonly used word?
Input Specification:
Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return \n
. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z
].
Output Specification:
For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.
Note that words are case insensitive.
Sample Input:
Can1: "Can a can can a can? It can!"
Sample Output:
can 5
题目大意:
在一行字符串中,提取出现次数最多的单词,单词中的字符只能有数字或字母。
解题思路:
遍历字符串,判断有效字符并且转为小写,将有效字符拼接到字符串并存储至map,最后遍历map,将元素封装成类,实现排序。也可以遍历时找最多的次数(字典序)。注意判断最后一个单词。
java代码:
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
StringBuilder builder = new StringBuilder();
Map<String,Integer> map = new HashMap<>();
List<Node1071> list = new ArrayList<Node1071>();
for(int i = 0; i < s.length();i++) {
char c = (s.charAt(i) + "").toLowerCase().charAt(0);
if(isFormat(c)) {
builder.append(c);
} else if (builder.length() > 0) {
String str = builder.toString();
Integer value = map.get(str);
if (value == null) {
map.put(str, 1);
} else {
map.put(str, value + 1);
}
builder.delete(0, builder.length());
}
}
if (builder.length() > 0) {
String str = builder.toString();
Integer value = map.get(str);
if (value == null) {
map.put(str, 1);
} else {
map.put(str, value + 1);
}
builder.delete(0, builder.length());
}
Set<String> keySet = map.keySet();
Iterator<String> iterator = keySet.iterator();
while(iterator.hasNext()) {
String key = iterator.next();
Integer value = map.get(key);
list.add(new Node1071(key, value));
}
Collections.sort(list);
System.out.println(list.get(0));
}
public static boolean isFormat(char c) {
if(c <= '9' && c >= '0' || c <= 'z' && c >= 'a' || c>= 'A' && c <= 'Z') {
return true;
}
return false;
}
}
class Node1071 implements Comparable<Node1071>{
String key;
int value;
public Node1071(String key, int value) {
this.key = key;
this.value = value;
}
@Override
public int compareTo(Node1071 o) {
if(this.value != o.value) {
return o.value - this.value;
}else {
return this.key.compareTo(o.key);
}
}
@Override
public String toString() {
return key + " " + value;
}
}