1071 Speech Patterns (25 分) java 题解

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

 

PTA提交截图:

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
《a little java, a few patterns》是一本由美国计算机科学家和教育家Matthew F. Austin和Michael Rogers所著的编程书籍。这本书结合了Java编程语言和设计模式的内容,为读者提供了一种新颖的编程方式。这本书的主要目标是帮助读者理解面向对象编程以及设计模式的理论和实践知识。 书中的内容包括Java语言的基本概念、面向对象编程的原则以及常用的设计模式。通过一些简单而有趣的例子,读者可以逐步学习如何使用Java编写面向对象的程序,并且了解如何应用设计模式来解决实际问题。书中还通过一种名为「菜鸟风格」的编程语言,让读者从一个简单而直观的角度来理解面向对象编程的核心思想和设计模式的应用。 《a little java, a few patterns》以其独特而有趣的教学方法备受好评。这本书不仅适合有一定编程基础的读者,也适合那些希望深入学习Java和设计模式的初学者。通过阅读这本书,读者可以提升自己的编程能力,掌握面向对象编程和设计模式的核心概念,并将其应用于实际的软件开发中。 总之,《a little java, a few patterns》是一本值得阅读的编程书籍。它既有助于读者理解Java编程语言和面向对象编程的基本原理,又教授了一些实用的设计模式。无论是初学者还是有一定编程经验的人,都可以从这本书中获得很多有价值的知识和经验。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值