zoukankan      html  css  js  c++  java
  • 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<cstdio>
     2 #include<iostream>
     3 #include<algorithm>
     4 #include<map>
     5 #include<vector>
     6 #include<string>
     7 using namespace std;
     8 map <string, int>mp;
     9 char str[1048577], word[500];
    10 vector<string> maxWord;
    11 bool cmp(string a, string b){
    12     return a < b;
    13 
    14 }
    15 int main(){
    16     int i,j, index, maxN = -1;
    17     string ss;
    18     char c;
    19     gets(str);
    20     int find;
    21     for(i = 0; str[i] != ''; i++){
    22         index = 0;
    23         while(str[i] >= 'a' && str[i] <= 'z' || str[i] >= 'A' && str[i] <= 'Z' || str[i] >= '0' && str[i] <= '9'){
    24             c = str[i++];
    25             if(c >= 'A' && c <= 'Z')
    26                 c = c - 'A' + 'a';
    27             word[index++] = c;
    28         }
    29         word[index] = '';
    30         if(index != 0){
    31             ss = word;
    32             int cnt;
    33             if(mp.count(ss) == 0){
    34                 mp[ss] = 1;
    35                 cnt = 1;
    36             }else{
    37                 mp[ss] = mp[ss] + 1;
    38                 cnt = mp[ss];
    39             }
    40             if(cnt > maxN)
    41                 maxN = cnt;
    42         }
    43     }
    44     for(map<string, int>::iterator it = mp.begin(); it != mp.end(); it++){
    45         if(it->second == maxN)
    46             maxWord.push_back(it->first);
    47     }
    48     sort(maxWord.begin(), maxWord.end(), cmp);
    49     for(int i = 0; i < maxWord.size(); i++){
    50         cout << maxWord[i] << " ";
    51     }
    52     printf("%d", maxN);
    53     cin >> i;
    54     return 0;
    55 }
    View Code

    总结:

    1、组装单词,将其用map<string, int> 记录出现次数, 并且记录最大值。当扫描完后,遍历map,找出所有出现次数等于最大值的单词,将其按字典排序输出。

    2、alphanumerical :字母和数字都算单词。

  • 相关阅读:
    (原)x264代码中的码流控制学习
    (原)关于OpenSL ES播放音频数据的一个奇怪的问题
    (原)理解码率控制模式(x264,x265,vpx)
    CentOS清除用户登录记录和命令历史方法
    2B销售提升业绩的5项技能
    Cookie中的httponly的属性和作用
    如何开展HW行动
    护网结束,复盘攻防,聊聊安全! 2019
    2018-11-21(34)《阿里铁军》阅后记!
    你了解渗透测试与红蓝队对抗的区别吗?
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8526199.html
Copyright © 2011-2022 走看看