• 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 }
  • 相关阅读:
    Mac 安装FFMpeg 与 FFmpeg 格式转换
    django channels
    python3 coroutine
    python中关于sql 添加参数
    python导包的问题
    python中的列表
    django中用model生成数据库表结构
    docker
    博客大神地址
    Bean复制的几种框架性能比较(Apache BeanUtils、PropertyUtils,Spring BeanUtils,Cglib BeanCopier)
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11307483.html
Copyright © 2020-2023  润新知