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 '\n'. 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 = ; i < s.size(); i++)
{
if(isalnum(s[i])) //isalnum检验是否是英文字符或者数字
{
tmp += tolower(s[i]);
}
if(!isalnum(s[i]) || i == s.size() - )
{
if(tmp.size() > )
dic[tmp]++;
tmp = "";
}
}
int curmax = ;
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;
}

Pat1071: Speech Patterns的更多相关文章

  1. PAT1071. Speech Patterns (25)

    题目要求的Word定义 Here a "word" is defined as a continuous sequence of alphanumerical characters ...

  2. 【算法笔记】A1071 Speech Patterns

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  3. PAT 1071 Speech Patterns[一般]

    1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For exam ...

  4. PAT 甲级 1071 Speech Patterns (25 分)(map)

    1071 Speech Patterns (25 分)   People often have a preference among synonyms of the same word. For ex ...

  5. PAT_A1071#Speech Patterns

    Source: PAT A1071 Speech Patterns (25 分) Description: People often have a preference among synonyms ...

  6. 1071 Speech Patterns——PAT甲级真题

    1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...

  7. 1071. Speech Patterns (25)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  8. Speech Patterns (string)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  9. A1071. Speech Patterns

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

随机推荐

  1. 《java入门第一季》之面向对象(成员方法)

    /* 类的组成:成员变量,成员方法 又加入了一个新的成员:构造方法. 以后再提(类的组成): 成员变量 构造方法 成员方法 根据返回值: void类型 非void类型 形式参数: 空参方法 非空参方法 ...

  2. linux的link命令

    sudo ln -s 源文件 目标文件 sudo ln -s /usr/local/mysql/bin/mysqladmin /sbin/mysqladmin 建立软连接 ln -d existfil ...

  3. nasm预处理器(4)

    nasm定义了一套标准宏,当开始处理源文件时,这些宏都已经被定义了,如果希望程序在执行前没有预定义的宏存在,可以使用%clear清空预处理器的一切宏. __NASM_MAJOR__ 主版本号 __NA ...

  4. 简单了解AJAX

    // ajax的俩个版本: var xmlhttp; if(window.xmlHttpRequest){ xmlhttp = new xmlHttpRequest(); }else{ xmlhttp ...

  5. python---01.名片管理系统

    这是第一篇文章,也是完整编写的第一份代码,,,,希望大神们多多指导,提出更好的想法. 第一部分-----提供选项的菜单栏 第二部分:根据用户输入的选择,提供功能 总体需要一个while True: 其 ...

  6. Salesforce Lightning开发学习(二)Component组件开发实践

    lightning的组件区分标准组件.自定义组件和AppExchange组件.标准组件由SF提供,自定义组件由developer自行开发,AppExchange组件由合作伙伴建立.下面我们写一个简单的 ...

  7. 推荐eclipse插件Properties Editor(转)

    Properties Editor 是一款properties文件编辑器. 需求:一般我们在做“国际化”功能时,我们需要properties中文表示方式用unicode表示.eclipse默认prop ...

  8. struts 开发流程

  9. struts2.1.8+hibernate2.5.6+spring3.0(ssh2三大框架)常见异常原因和解决方案

    ---------------------------------------------------------------------------------------------------- ...

  10. HashMap 深入分析

    /**     *@author annegu     *@date 2009-12-02     */ Hashmap是一种非常常用的.应用广泛的数据类型,最近研究到相关的内容,就正好复习一下.网上 ...