• 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;
    }
  • 相关阅读:
    transform:translate -50%解释
    无缝滚动条
    css二维画面练习-扑克牌
    css二维动画
    ORM了解
    socket总结
    2016/9/23总结电脑内容
    winform控件跨线程委托
    HttpRequestMessage
    vue设置每个页面的头部title
  • 原文地址:https://www.cnblogs.com/0kk470/p/7739551.html
Copyright © 2020-2023  润新知