zoukankan      html  css  js  c++  java
  • A1071. 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
     1 #include <stdio.h>
     2 #include <stdlib.h>
     3 #include <iostream>
     4 #include <string.h>
     5 
     6 #include <math.h>
     7 #include <algorithm>
     8 #include <vector>
     9 #include <set> 
    10 #include <string>
    11 #include <map> 
    12 using namespace std;
    13 
    14 bool check(char a)
    15 {
    16     if(a>='A'&&a<='Z')return true;
    17     if(a>='a'&&a<='z')return true;
    18     if(a>='0'&&a<='9')return true;
    19     return false;
    20 }
    21 
    22  
    23 int main(){
    24    string a,b;
    25    getline(cin,a);
    26    map<string,int> count;
    27    int i=0;
    28    while(i<a.length())
    29    {
    30        b="";
    31        while(i<a.length()&&check(a[i])==true)
    32        {
    33            if(a[i]>='A'&&a[i]<='Z')a[i]=a[i]+'a'-'A';
    34            b+=a[i];
    35            i++;
    36        }
    37        if(b!="")
    38        {
    39               if(count.find(b)!=count.end())
    40               {
    41                   count[b]++;
    42               }else count[b]=1;
    43        }
    44        //过滤非有效字符
    45       while(i<a.length()&&check(a[i])==false) i++;
    46    }
    47    string ans;
    48    int max=0;
    49    for(map<string,int>::iterator it=count.begin();it!=count.end();it++)
    50    {
    51        if(it->second>max)
    52        {
    53            max=it->second;
    54         ans=it->first; 
    55        }
    56    }
    57    cout<<ans<<" "<<max<<endl;
    58     return 0;
    59 }
  • 相关阅读:
    第二章 PROCESSES AND THREADS
    第一章 PROBLEMS
    第10章 图 10.1
    day2_jmeter关联的两种实现方式
    jmeter做SOAPui接口的性能测试
    day1_json_viewer美化接口请求、接口用例设计的方式和接口测试的必要性
    day1_jmeter接口如何添加断言
    day1_jmeter操作mysql步骤
    day1_jmeter添加cookie管理器和header信息头管理器
    day1_postman和jmeter处理接口入参既有key-value类型,也有上传文件类型的方式,利用postman实现自动化
  • 原文地址:https://www.cnblogs.com/ligen/p/4308917.html
Copyright © 2011-2022 走看看