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 :字母和数字都算单词。

  • 相关阅读:
    SqlSession接口和Executor
    MySQL 存储表情字符
    Lisp学习--Windows下面的开发环境搭建
    使用反射+缓存+委托,实现一个不同对象之间同名同类型属性值的快速拷贝
    GIT团队合作探讨之一-保持工作同步的概念和实践
    关于IE8下media query兼容的解决方案探讨
    git下的团队合作模型及git基础知识汇集
    互联网环境下服务提供的模式
    web统计数据搜集及分析原理
    网站统计及移动应用数据统计相关术语知识详解
  • 原文地址:https://www.cnblogs.com/zhuqiwei-blog/p/8526199.html
Copyright © 2011-2022 走看看