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

  • 相关阅读:
    Hibernate4学习day0--hibernate封装--注解--单元测试
    Hibernate4学习day01--简介--基本配置
    java基础day13---引用数据类型
    java基础day14---static关键字-----继承
    java基础day12---this 关键字-----参数传递
    day05 Struts2文件上传和下载---防重复提交
    java基础day11---空指针异常----引用类型--自定义类型赋值--封装
    java基础的第二轮快速学习!day10
    Struts2,大爷你好!第四天
    java基础的第二轮快速学习!day09
  • 原文地址:https://www.cnblogs.com/MartinLwx/p/12832813.html
Copyright © 2011-2022 走看看