zoukankan      html  css  js  c++  java
  • PAT甲级——A1071 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

     1 #include <iostream>
     2 #include <map>
     3 #include <string>
     4 using namespace std;
     5 int main()
     6 {
     7     string str, s = "", res;
     8     map<string, int>words;
     9     int maxN = -1;
    10     getline(cin, str);
    11     for (int i = 0; i < str.length(); ++i)
    12     {
    13         if(isalnum(str[i])!=0)//数字也算
    14             s += tolower(str[i]);
    15         else if(s.length()>0)
    16         {
    17             words[s]++;
    18             s = "";
    19         }
    20     }
    21     //注意,不要漏了最后一个单词
    22     if (!s.empty())
    23         words[s]++;
    24     for (auto ptr = words.begin(); ptr != words.end(); ++ptr)
    25     {
    26         if (maxN < ptr->second)
    27         {
    28             maxN = ptr->second;
    29             res = ptr->first;
    30         }
    31     }
    32     cout << res << " " << maxN << endl;
    33     return 0;
    34 }
  • 相关阅读:
    如何在Element 使用正则表达式校验
    Vs Code 微信小程序 神兵利器合集
    分享CSS公共类库(能在项目快捷使用CSS类)
    Element+Axios上传图片 OR 文件
    两种简洁的数组对象去重姿势
    VScode 格式化代码保存时使用ESlint修复代码
    如何在linux上部署vue项目
    Vue 2.x 3.x 配置项目开发环境跟线上环境
    在Vue文件中引入外部URL链接
    博客目录
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11307483.html
Copyright © 2011-2022 走看看