zoukankan      html  css  js  c++  java
  • 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
    
    代码:
    #include <cstdio>
    #include <iostream>
    #include <algorithm>
    #include <cstring>
    #include <map>
    using namespace std;
    
    int main()
    {
        char ch,str[10];
        map<string,int> p;
        string s;
        int i = 0,j,m = 0;
        do
        {
            ch = getchar();
            if(isalnum(ch))
            {
                if(isupper(ch))
                {
                    str[i ++] = ch + 32;
                }
                else str[i ++] = ch;
            }
            else if(i != 0)
            {
                str[i] = '';
                i = 0;
                p[str] ++;
                if(m < p[str])m = p[str],s = str;
                else if(m == p[str] && s > str)s = str;
            }
        }
        while(ch != '
    ');
        cout<<s<<' '<<m;
    }
  • 相关阅读:
    wtl 支持托拽文件并在ListBox框中显示文件路径的方法
    提升本程序进程优先级和权限(VC++源代码)
    Win7下运行VC程序UAC权限问题
    Flex中带for的循环
    Flex注释
    Flex建立编译环境
    Flex事件驱动机制
    Flex应用程序的系统开发周期
    Linux下c开发 之 线程通信
    将 Win32 C/C++ 应用程序迁移到 POWER 上的 Linux,第 1 部分: 进程、线程和共享内存服务
  • 原文地址:https://www.cnblogs.com/8023spz/p/8214977.html
Copyright © 2011-2022 走看看