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 }
  • 相关阅读:
    Redis5设计与源码分析 (第17章 HyperLogLog相关命令的实现)
    Redis5设计与源码分析 (第16章 GEO相关命令)
    ES5和ES6函数的this指向
    vue响应式原理 (响应式并不等于数据双向绑定,千万不要混淆)
    vue中data为什么是函数而不是对象
    vue-enum 前端常量 枚举工具
    Vue3 写业务逻辑不适合用TS(TypeScript)
    vue-property-decorator vue3 ts 用的装饰器
    github git clone下载加速 && npm install 下载加速
    vue3 如果用ts,导出时候要用 defineComponent,这俩是配对的,为了类型的审查正确
  • 原文地址:https://www.cnblogs.com/chunlinn/p/10751787.html
Copyright © 2011-2022 走看看