zoukankan      html  css  js  c++  java
  • PAT 1071 Speech Patterns (25)

    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

    出现标点符号的时候, 用空格代替这个标点符号, 否则可能出错
    比如输入字符串为aa'a'a'a' 如果不把标点符号同空格代替, 输出的就是aaaa 1 而不是a 4
    此外, 在字符串的末尾添加一个空格 便于提取最后一个词
     1 #include<iostream>
     2 #include<string>
     3 #include<map>
     4 using namespace std;
     5 int main(){
     6   string s;
     7   int i, cnt=0;
     8   getline(cin, s);//大写字母转小写字母, 标点符号替换为空格
     9   for(i=0; i<s.size(); i++) {
    10     if(s[i]<='Z' && s[i]>='A') s[i] += 32;
    11     else if(!((s[i]>='0' && s[i]<='9') || (s[i]>='a' && s[i]<='z'))) s[i] = ' ';
    12   }
    13   //在末尾添加一个空格, 方便最后一个单词的提取
    14   s.append(" ");
    15   map<string, int> m;
    16   int begin=0, maxn=-1;
    17   string ans="";
    18   for(i=0; i<s.size();){
    19       if(s[i]==' '){//提取每个单词
    20         string temp = s.substr(begin, i-begin);
    21         int num = ++m[temp];
    22         if(maxn<num){
    23             ans = temp;
    24             maxn = num;
    25         }else if(maxn==num && ans>temp) ans=temp;
    26         while(i<s.size() && s[i]==' ') i++;
    27         begin = i;
    28       }
    29       else i++;
    30   }
    31   cout<<ans<<" "<<maxn<<endl;
    32   return 0;
    33 }
    有疑惑或者更好的解决方法的朋友,可以联系我,大家一起探讨。qq:1546431565
  • 相关阅读:
    对C#中的Close()和Dispose()的浅显理解
    SqlParameter类中的两对好基友:SqlDbType与DbType、SqlValue与Value
    C#通过获取快捷方式指向目标的小示例触碰WMI
    小心UAC
    【TSQL】获取指定日期的常用前后节点(月初月末周一周末等等)
    弹出移动设备时报正在使用肿么办
    再获殊荣!霍格沃兹荣获腾讯金课堂「教育突破奖」
    实战 | 电商业务的性能测试(一): 必备基础知识
    接口测试框架实战(二)| 接口请求断言
    测试老鸟总结的 16 个测试改进 Tips ,让你少走弯路!
  • 原文地址:https://www.cnblogs.com/mr-stn/p/9231304.html
Copyright © 2011-2022 走看看