PAT(甲级)1071
来源:互联网 发布:阿里云centos7环境php 编辑:程序博客网 时间:2024/06/06 16:02
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 '\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
#include <iostream>#include <cstring>#include <cctype>#include <map>#define SIZE 1048600using namespace std;map<string,int> m;char str[SIZE];//////////////////////////////////////////////////////////////////the convertion between char string and string///////////////////////////////////////////////////////////////int main(){//freopen("test.txt","r",stdin);gets(str);int max=0;string strtmp;char *p =str,*pstart=NULL;while(*p != '\0'){while(!isalnum(*p) && *p !='\0') //SKIP NON-ALPHNUMBERS p++;if(*p == '\0') //if terminated,break; break;pstart = p;while(isalnum(*p)){if(*p >='A' && *p <= 'Z') *p = *p-'A'+'a'; p++;}if(*p != '\0'){ //guarantee on overflow *p = '\0'; p++;}string s(pstart);m[s]++;if(m[s] > max){strtmp = s;max = m[s];}else if(m[s] == max){if(strtmp > s) strtmp =s;}}printf("%s %d",strtmp.c_str(),max);//fclose(stdin);return 0;}
- PAT(甲级)1071
- PAT甲级1071
- PAT 甲级
- 浙大PAT甲级 1071 字符串计数
- PAT甲级 A1025.PAT RANKING
- PAT 甲级 1025 PAT Ranking
- PAT(甲级)1003
- PAT(甲级)1004
- PAT(甲级)1005
- PAT(甲级)1006
- PAT(甲级)1007
- PAT(甲级)1008
- PAT(甲级)1009
- PAT(甲级)1010
- PAT(甲级)1011
- PAT(甲级)1012
- PAT(甲级)1013
- PAT(甲级)1014
- 备份几篇文章
- Linux内核中的常用数据结构-双向链表(1)
- JSON字符串反序列化为MAP对象开发实例
- 我的进阶曲线之二
- jquery中attr和prop的区别
- PAT(甲级)1071
- 【读书笔记】iOS-NSPredicate
- 一张图告诉你什么是JNI
- 字符转换属性text-tranform改变大小写
- Linux下Apache服务的启动与关闭
- iOS app 上传到itunes connect的时候一直都是正在准备(二进制文件无效)
- php学习00
- 我的进阶曲线之三
- PAT(甲级)1072