1071 Speech Patterns

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:

  1. Can1: "Can a can can a can? It can!"

Sample Output:

  1. can 5

题目大意:给你一组字符串让你找出这组字符串中出现单词个数最多的单词,并输出其次数。

大致思路:用 map<string,int> 来记录每一个单词的出现次数,记住map会自动对插入的单词按照字典序排序,最后输出的时候没必要在特判一下。在对字符串划分单词的时候如果这个字符是字母或者数字就添加到单词中,如果不是就切割并且统计出现次数,同时注意空字符不予以统计(这个地方卡了很久一定要注意)。C++提供了 isalnum() 函数用来判断字符和数字没必要自己在写一个函数。

代码:

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. map<string, int> dict; //map会自动按照字典序排序
  4. bool check(char ch) {
  5. if (ch >= '0' && ch <= '9') return true;
  6. if (ch >= 'a' && ch <= 'z') return true;
  7. if (ch >= 'A' && ch <= 'Z') return true;
  8. return false;
  9. }
  10. int main() {
  11. string str;
  12. getline(cin, str);
  13. // getchar();
  14. string tmp = "";
  15. for (int i = 0; i < str.size(); i++) {
  16. if (check(str[i])) {
  17. str[i] = tolower(str[i]);
  18. tmp += str[i];
  19. }
  20. if (!check(str[i]) || i == str.length() - 1) {
  21. if (tmp.length() != 0) dict[tmp]++;
  22. tmp = "";
  23. }
  24. }
  25. int ans1 = 0; string ans2 = "";
  26. for (auto ite : dict) {
  27. if (ite.second > ans1) {
  28. ans1 = ite.second;
  29. ans2 = ite.first;
  30. }
  31. }
  32. cout << ans2 << " " << ans1 << endl;
  33. return 0;
  34. }

1071 Speech Patterns——PAT甲级真题的更多相关文章

  1. PAT 甲级真题题解(63-120)

    2019/4/3 1063 Set Similarity n个序列分别先放进集合里去重.在询问的时候,遍历A集合中每个数,判断下该数在B集合中是否存在,统计存在个数(分子),分母就是两个集合大小减去分 ...

  2. PAT 甲级真题题解(1-62)

    准备每天刷两题PAT真题.(一句话题解) 1001 A+B Format  模拟输出,注意格式 #include <cstdio> #include <cstring> #in ...

  3. 1080 Graduate Admission——PAT甲级真题

    1080 Graduate Admission--PAT甲级练习题 It is said that in 2013, there were about 100 graduate schools rea ...

  4. PAT甲级真题及训练集

    正好这个"水水"的C4来了 先把甲级刷完吧.(开玩笑-2017.3.26) 这是一套"伪题解". wacao 刚才登出账号测试一下代码链接,原来是看不到..有空 ...

  5. PAT 甲级真题

    1019. General Palindromic Number 题意:求数N在b进制下其序列是否为回文串,并输出其在b进制下的表示. 思路:模拟N在2进制下的表示求法,“除b倒取余”,之后判断是否回 ...

  6. PAT甲级真题 A1025 PAT Ranking

    题目概述:Programming Ability Test (PAT) is organized by the College of Computer Science and Technology o ...

  7. Count PAT's (25) PAT甲级真题

    题目分析: 由于本题字符串长度有10^5所以直接暴力是不可取的,猜测最后的算法应该是先预处理一下再走一层循环就能得到答案,所以本题的关键就在于这个预处理的过程,由于本题字符串匹配的内容的固定的PAT, ...

  8. 1018 Public Bike Management (30分) PAT甲级真题 dijkstra + dfs

    前言: 本题是我在浏览了柳神的代码后,记下的一次半转载式笔记,不经感叹柳神的强大orz,这里给出柳神的题解地址:https://blog.csdn.net/liuchuo/article/detail ...

  9. 1022 Digital Library——PAT甲级真题

    1022 Digital Library A Digital Library contains millions of books, stored according to their titles, ...

随机推荐

  1. docker(6)镜像的使用

    前言 Docker的三大核心概念:镜像.容器.仓库.初学者对镜像和容器往往分不清楚,学过面向对象的应该知道类和实例,这跟面向对象里面的概念很相似 我们可以把镜像看作类,把容器看作类实例化后的对象. d ...

  2. 使用kubekey安装kubesphere

    下载 KubeKey KubeKey 是新一代 Kubernetes 和 KubeSphere 安装器,可帮助您以简单.快速.灵活的方式安装 Kubernetes 和 KubeSphere. expo ...

  3. JMM和volatile

    1.volatile 2.JMM 3.代码示例 package com.yanshu; class MyNmuber{ volatile int number=10; public void addT ...

  4. 《Proxy系列专题》:代理模式(静态、JDK、CGLib)

    <Proxy系列专题>:代理模式(静态.JDK.CGLib)使用 现象:在如今互联网时代,项目的复杂度不断的提升,有些场景下需要一定的设计优化来支撑业务的扩展,如为了不改动原始类,但需要对 ...

  5. AtCoder Beginner Contest 178

    比赛链接:https://atcoder.jp/contests/abc178/tasks A - not 题意 给出一个整数 $0 \le x \le 1$,如果 $x$ 是 $0$ 就输出 $1$ ...

  6. 【noi 2.6_3531】判断整除(DP)

    题意:给一个正整数数列,可将其相加或相减,问是否有一个结果能被K整除. 解法:似上一题"糖果"的状态定义,f[i][j]表示是否有一个选了前 i 个数的结果模K余j. P.S. 可 ...

  7. [CodeForces-629A 用阶乘会爆掉

    题意: 给你一个n*n的蛋糕,如果某个位置是'C'那就代表这是一个巧克力块,否则就不是.如果某两个巧克力块在同一行或同一列,那么这个家庭的幸福值就会加1,问你这个家庭的幸福值最大是多少 Input 3 ...

  8. Codeforces Round #643 (Div. 2) B. Young Explorers (思维,贪心)

    题意:给你一组人\(a\),现在要将这些人进行分组,对于\(i\),只有某一组的人数\(\ge a_{i}\)时,\(i\)才可以加入这个组,问最多能够有多少组,(不必将所有人都选用). 题解:我们将 ...

  9. Educational Codeforces Round 89 (Rated for Div. 2) C. Palindromic Paths (思维)

    题意:有一个\(n\)x\(m\)的矩阵,从\((1,1)\)出发走到\((n,m)\),问最少修改多少个数,使得所有路径上的数对应相等(e.g:\((1,2)\)和\((n-1,m)\)或\((2, ...

  10. Unmanaged Exports not creating a .lib file

    别用VS2017!别用VS2017!别用VS2017!去吧.