zoukankan      html  css  js  c++  java
  • 【算法笔记】A1071 Speech Patterns

    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

    题意

      统计一句话中出现次数最多的单词,输出这个单词和出现次数。注意can1也算一个单词。

    思路

      步骤1:把每个单词从句子里分离出来。

      步骤2:用map记录出现次数。

      步骤3:遍历map,输出次数最多的单词。

    code:

     1 #include<bits/stdc++.h>
     2 using namespace std;
     3 map<string, int> wordStat;
     4 bool check(char c){
     5     if(c>='0'&&c<='9') return true;
     6     if(c>='A'&&c<='Z') return true;
     7     if(c>='a'&&c<='z') return true;
     8     return false;
     9 }
    10 int main(){
    11     string str, pattern;
    12     int i = 0, max = 0;
    13     getline(cin, str);
    14     while(i < str.length()){
    15         string word;
    16         while(i < str.length() && check(str[i])==true){
    17             if(str[i]>='A'&&str[i]<='Z') str[i] += 32;
    18             word += str[i];
    19             i++;
    20         }
    21         if(word != ""){
    22             if(wordStat.find(word) == wordStat.end()) wordStat[word] = 1;
    23             else wordStat[word]++;
    24         }
    25         while(i < str.length() && check(str[i])==false){
    26             i++;
    27         }
    28     }
    29     for(map<string, int>::iterator it = wordStat.begin(); it!=wordStat.end(); it++){
    30         if(max <= it->second){
    31             max = it->second;
    32             pattern = it->first;
    33         }
    34     }
    35     cout<<pattern<<" "<<max;
    36     return 0;
    37 }
  • 相关阅读:
    show()的几种方法
    sql的交叉连接,内连接,左外连接,右外连接,全外连接总结
    MySQL数据库引擎介绍、区别、创建和性能测试的深入分析
    RedHat9通过Host-only配置网络连接
    解决Struts2.2.20版本的标签不支持style属性的问题
    SQL查询数据库中所有含有某一列的所有表
    简单工厂模式
    vc常用类总结(转载)
    嵌入式程序员应知道的0x10个C语言Tips
    C语言位操作
  • 原文地址:https://www.cnblogs.com/chunlinn/p/10751787.html
Copyright © 2011-2022 走看看