zoukankan      html  css  js  c++  java
  • Pat1071: Speech Patterns

    1071. Speech Patterns (25)

    时间限制
    300 ms
    内存限制
    65536 kB
    代码长度限制
    16000 B
    判题程序
    Standard
    作者
    HOU, Qiming

    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

    思路

    找字符串中符合要求且出现次数最多的单词,用map模拟一个字典统计出现的单词就可以了。
    注:注意最后一位如果是数字或者字母要再向字典插入一次。

    代码
    #include<iostream>
    #include<map>
    #include<ctype.h>
    #include<iterator>
    using namespace std;
    int main()
    {
        string s;
        map<string,int> dic;
        getline(cin,s);
        string tmp;
        for(int i = 0; i < s.size(); i++)
        {
            if(isalnum(s[i]))  //isalnum检验是否是英文字符或者数字
            {
                tmp += tolower(s[i]);
            }
            if(!isalnum(s[i]) || i == s.size() - 1)
            {
                if(tmp.size() > 0)
                   dic[tmp]++;
                tmp = "";
            }
        }
        int curmax = 0;
        for(map<string,int>:: iterator it = dic.begin(); it != dic.end(); it++)
        {
            if(it->second > curmax)
            {
                tmp = it->first;
                curmax = it->second;
            }
        }
        cout << tmp << " " << curmax << endl;
    }
  • 相关阅读:
    Redis缓存穿透,缓存击穿,缓存雪崩
    Redis持久化机制
    Docker小白到实战之常用命令演示,通俗易懂
    分布式事务最终一致性-CAP框架轻松搞定
    gRPC四种模式、认证和授权实战演示,必赞~~~
    Docker小白到实战之开篇概述
    郑州 | 7月20日,想想都后怕
    避不开的分布式事务
    c++实现十大经典排序算法
    浏览器缓存机制总结
  • 原文地址:https://www.cnblogs.com/0kk470/p/7739551.html
Copyright © 2011-2022 走看看