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 }
  • 相关阅读:
    jQuery插件之jquery editable plugin点击编辑文字插件
    firefox与ie的javascript兼容性编程汇编【转载】
    css前端制作 经验总结
    非常棒的jqChart图表插件
    WPF Image Source设置文件路径后 在编辑状态下显示图片,运行时不显示
    WPF RadioButton 绑定枚举
    WPF MVVM实现数据增删改查逻辑全流程详细解析demo
    bigNumber.js的简单使用
    PHP程序的“Missing argument 3”的错误提示解决方法
    PHP判断0和空的方法
  • 原文地址:https://www.cnblogs.com/zzw1024/p/11307483.html
Copyright © 2011-2022 走看看