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;
    }
  • 相关阅读:
    ES5新特性:理解 Array 中增强的 9 个 API
    ios
    Jquery异步 Deferred Object
    ES5中新增的Array方法详细说明
    Chart
    Angular常用语句
    vticker.js--垂直滚动插件
    <css系列>之css--float总结
    理解boot.img与静态分析Android/linux内核
    理解竞争条件( Race condition)漏洞
  • 原文地址:https://www.cnblogs.com/0kk470/p/7739551.html
Copyright © 2011-2022 走看看