PAT甲级1071
来源:互联网 发布:mac 任务管理工具 编辑:程序博客网 时间:2024/06/05 10:44
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<cstdio>#include<cstring>#include<map>#include<string>#include<algorithm>using namespace std;char a[1048576 + 10];int main(){gets_s(a);int i = 0;int len = strlen(a);map<string, int> ma;string s = "";while (i < len){s = "";while (a[i] >= '0'&&a[i] <= '9' || a[i] >= 'a'&&a[i] <= 'z' || a[i] >= 'A'&&a[i] <= 'Z'){if (a[i] >= 'A'&&a[i] <= 'Z'){a[i] = a[i] - 'A' + 'a';}s += a[i];i++;if (i == len)break;}ma[s]++;i++;}int max = -1; string maxstring;for (map<string, int>::iterator it = ma.begin(); it != ma.end(); it++){if (it->second > max&&it->first!=""){max = it->second;maxstring = it->first;}}printf("%s %d",maxstring.c_str(),max);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
- LeetCode 257. Binary Tree Paths
- 小学生算术
- [BZOJ4551][Tjoi2016&Heoi2016]树 dfs序+线段树
- HDU 1043 Eight ((八数码问题)逆向BFS + 康托定理判重)
- leetcode 随机概率 水塘抽样
- PAT甲级1071
- 2016小结
- Altium Desinger 的基本使用(2)
- 9.2.3
- css 如何实现导航border-bottom 有阴影的感觉
- POJ 2773 Happy 2006 【gcd的性质(也可用欧拉函数)】
- Jenkins 升级到2.42 Junit test report 出现错误的解决
- HDU1556:Color the ball(线段树区间更新)
- java基础六对象