zoukankan      html  css  js  c++  java
  • PTA(Advanced Level)1071.Speech Patterns

    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 . 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<string, int>,会默认按照string排序,免去了写排序的烦恼

    代码

    #include<bits/stdc++.h>
    using namespace std;
    map<string, int> word_list;
    int main()
    {
    	char c;
    	string tmp = "";
    	while(~scanf("%c", &c))
    	{
    		if(isalpha(c) || isdigit(c))
            {
                if(isupper(c))  c = tolower(c);		// 大小写不敏感,所以是按照小写存储
                tmp += c;
            }
    		else
    		{	
    		    if(tmp != "")	// 遇到连续的非字母/数字字符串就会是空字符串,不统计空字符串
                    if(word_list.count(tmp) == 0)
                        word_list[tmp] = 1;
                    else
                        word_list[tmp]++;
    			tmp = "";
    		}
    	}
    	int max_num = 0;
    	string ans;
    	map<string, int>::iterator it;
    	for(it=word_list.begin();it!=word_list.end();it++)		// 找出出现次数最多的
    	{
    		if(it->second > max_num)
    		{
    			max_num = it->second;
    			ans = it->first;
    		}
    	}
    	cout << ans << " " << max_num;
    	return 0;
    }
    

    引用

    https://pintia.cn/problem-sets/994805342720868352/problems/994805398257647616

  • 相关阅读:
    模拟器安装.apk包_夜神模拟器
    SDK安装报错_2019
    Jenkins安装插件方法
    Jenkins安装
    Python项目第三方库安装_pip freeze命令
    深入理解Java虚拟机—内存分配
    深入理解Java虚拟机—垃圾回收 下
    深入理解Java虚拟机—垃圾回收 上
    深入理解Java虚拟机—OutOfMemoryError异常
    深入理解Java虚拟机—Java内存区域
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/12832813.html
Copyright © 2011-2022 走看看