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 }
  • 相关阅读:
    机械迷城MAC下载及攻略
    今晚是个难眠之夜
    div高度自适应
    代码高亮
    windows live writer
    Java连接redis的使用示例
    luogu4360 锯木厂选址 (斜率优化dp)
    poj1651 Multiplication Puzzle (区间dp)
    hdu3506 Monkey Party (区间dp+四边形不等式优化)
    poj1236/luogu2746 Network of Schools (tarjan)
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11307483.html
Copyright © 2011-2022 走看看