PTA1071 - Speech Patterns - map计算不同单词个数
题意
输出给定字符串出现最多的字符串(小写输出)和出现次数。
所求字符串要求:字符中可以含有A-Z、0-9。
比如说题目给出的Can1,我们可以转换成can1,can1就算一个字符串整体,而不是单独的取出can1里面的can来加一。
思路
先把大写字母全部转换成小写,然后再用map存储单词个数即可,简单用法。
注意
有个特殊的数据要判断,不写的话牛客可以AC,但是PTA最后一组数据会报错。
特例:给出的字符串最后一个字符如果不是'0'-'9'、'a'-'z'的话,单独算一个字符串判断。
for循环的时候注意判断空字符串(空格)出现的情况
也可以用sstreamstring来处理一下,都可以的
AC代码
#include<iostream>
#include<string.h>
#include<cmath>
#include<map>
using namespace std;
#define inf 0x3f3f3f3f
typedef long long ll;
map<string,int> mp;
int main()
{
string s;
getline(cin,s);
int l=s.length();
for(int i=0;i<l;i++)
{
if(s[i]>='A'&&s[i]<='Z')
s[i]+=32;
}
string ss;
for(int i=0;i<l;i++)
{
if((s[i]>='0'&&s[i]<='9')||(s[i]>='a'&&s[i]<='z'))
ss+=s[i];
else if(ss!="")
mp[ss]++,ss="";
}
ss=s[l-1]; // 最后一个字符特判
if(!(s[l-1]>='0'&&s[l-1]<='9')||!(s[l-1]>='a'&&s[l-1]<='z'))
mp[ss]++;
string ans;
int ma=0;
for(auto&i:mp)
{
if(i.second>ma)
ma=i.second,ans=i.first;
}
cout<<ans<<" "<<ma<<endl;
return 0;
}
PTA1071 - Speech Patterns - map计算不同单词个数的更多相关文章
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 【ACM小白成长撸】--计算单词个数
我判断单词个数的方法,根据空格‘ ’的个数 分情况 当没有单词的时候 判断第一个符号,即a[0] == ‘\0’时,赋值给存储个数的数组 当遇到空格时,只有前面一个字符不是空格字符,后面一个字符不是空 ...
- 1071 Speech Patterns——PAT甲级真题
1071 Speech Patterns People often have a preference among synonyms of the same word. For example, so ...
- Pat1071: Speech Patterns
1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming Peo ...
- 【算法笔记】A1071 Speech Patterns
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- NOIP200107统计单词个数
NOIP200107统计单词个数 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给出一个长度不超过200的由 ...
- NOIP2001 统计单词个数
题三 统计单词个数(30分) 问题描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k&l ...
- kwic--Java统计单词个数并按照顺序输出
2016-07-02(随笔写作时间) 写了好久的程序了为了避免以后用到.......... 是一个统计单词个数,并按照个数从大到小输出的.输入文件名OK 了 单词是按照首字母排序的,,,里面用到映射等 ...
- PAT 1071 Speech Patterns[一般]
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For exam ...
随机推荐
- NCD 2019 M. NCD Salary
题意 :给你两个指数类型的数\(A^m\)和\(B^n\),比较他们的大小.保证底数和指数中最多只有一个为0. 题解 :题目数据非常大,肯定不能直接比较.由换底公式和\(Log\)函数的性质我们知道: ...
- Codeforces Round #521 (Div. 3) E. Thematic Contests (离散化,二分)
题意:有\(n\)个话题,每次都必须选取不同的话题,且话题数必须是上次的两倍,第一次的话题数可以任意,问最多能选取多少话题数. 题解:我们首先用桶来记录不同话题的数量,因为只要求话题的数量,与话题是多 ...
- git branch & git remote branch
git branch & git remote branch $ git branch -h usage: git branch [<options>] [-r | -a] [-- ...
- flex 布局占位符
flex 布局占位符 空 span bug .popover-custom-class .system-guide-container .buttons-box { display: flex; fl ...
- CSS hover box
CSS hover box transition 踩坑指南, display: none; 作为初始状态,不会产生动画效果,必须设置 height: 0; 或 width: 0; 来实现隐藏! tra ...
- 读写 LED 作业 台灯的 频闪研究 2 评测&对比!
0. 读写 LED 作业 台灯的 频闪研究 2 评测&对比! 评测&对比图: 1. 日光:(中午12点) 2. Philips: (天猫 15元 5w E27白) 3. FSL: ( ...
- nodemon all in one
nodemon all in one https://nodemon.io/ https://github.com/remy/nodemon#nodemon https://www.npmjs.com ...
- 如何在 macOS 上进行滚动截屏
如何在 macOS 上进行滚动截屏 Shift-Command-5 https://support.apple.com/zh-cn/guide/mac-help/mh26782/mac demo Xn ...
- how to write string to file in bash
how to write string to file in bash https://stackoverflow.com/questions/22713667/shell-script-how-to ...
- js add Struct to ArrayBuffer
使用struct-buffer为ArrayBuffer添加结构体 $ npm i struct-buffer 1. 创建结构体 import { DWORD, string_t, StructBuff ...