题意

输出给定字符串出现最多的字符串(小写输出)和出现次数。

所求字符串要求:字符中可以含有A-Z、0-9。

比如说题目给出的Can1,我们可以转换成can1,can1就算一个字符串整体,而不是单独的取出can1里面的can来加一。

思路

先把大写字母全部转换成小写,然后再用map存储单词个数即可,简单用法。

注意

  1. 有个特殊的数据要判断,不写的话牛客可以AC,但是PTA最后一组数据会报错。

    特例:给出的字符串最后一个字符如果不是'0'-'9'、'a'-'z'的话,单独算一个字符串判断。

  2. for循环的时候注意判断空字符串(空格)出现的情况

  3. 也可以用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计算不同单词个数的更多相关文章

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

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

  2. 【ACM小白成长撸】--计算单词个数

    我判断单词个数的方法,根据空格‘ ’的个数 分情况 当没有单词的时候 判断第一个符号,即a[0] == ‘\0’时,赋值给存储个数的数组 当遇到空格时,只有前面一个字符不是空格字符,后面一个字符不是空 ...

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

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

  4. Pat1071: Speech Patterns

    1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming Peo ...

  5. 【算法笔记】A1071 Speech Patterns

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

  6. NOIP200107统计单词个数

    NOIP200107统计单词个数 难度级别: A: 编程语言:不限:运行时间限制:1000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给出一个长度不超过200的由 ...

  7. NOIP2001 统计单词个数

    题三 统计单词个数(30分) 问题描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份(1<k&l ...

  8. kwic--Java统计单词个数并按照顺序输出

    2016-07-02(随笔写作时间) 写了好久的程序了为了避免以后用到.......... 是一个统计单词个数,并按照个数从大到小输出的.输入文件名OK 了 单词是按照首字母排序的,,,里面用到映射等 ...

  9. PAT 1071 Speech Patterns[一般]

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

随机推荐

  1. Codeforces Beta Round #92 (Div. 2 Only) B. Permutations

    You are given n k-digit integers. You have to rearrange the digits in the integers so that the diffe ...

  2. Java RMI 实现一个简单的GFS(谷歌文件系统)——介绍篇

    本系列主要是使用Java RMI实现一个简单的GFS(谷歌文件系统,google file system),首先整体简单介绍下该项目. [为了更好的阅读以及查看其他篇章,请查看原文:https://w ...

  3. windows10 浏览器跑分对比!

    2015-12-12 windows10 浏览器跑分对比! YOUR BROWSER SCORES!                MaxScore=555http://html5test.com/i ...

  4. Ajax & JSONP 原理

    Ajax & JSONP 原理 AJAX不是JavaScript的规范,它只是一个哥们"发明"的缩写:Asynchronous JavaScript and XML,意思就 ...

  5. web online code editor All In One

    web online code editor All In One 在线代码编辑器 Monaco Editor 摩纳哥编辑器 ️ 22.1k The Monaco Editor is the code ...

  6. SVG tada &#127881; animation effects

    SVG tada animation effects symbol & use <svg viewBox="0 0 80 20" xmlns="http:/ ...

  7. git & Angular git commit 规范

    git & Angular git commit 规范 https://github.com/angular/angular/commits/master https://github.com ...

  8. image to cur (cursor icons)

    image to cur (cursor icons) mouse-cursor-pointer https://onlineconvertfree.com/convert-format/jpg-to ...

  9. 图解 git 流程

    图解 git 流程 Github 开源项目 1 动画 2 web repl 3 online git cli & create remote branch # Create a new bra ...

  10. Windows Server2012 r2 nginx反向代理图片服务器

    1.下载nginx  http://nginx.org/en/download.html,本人下载的1.18.0版本 2.下载 Windows Service Wrapper(winsw.exe) v ...