题目要求的Word定义 Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.  所以 can""can""can 中应该是3个单词

#include <iostream>
#include <cctype>
#include <map>
using namespace std;
map<string,int> m;
void trims(string &s){
int isFront=1,isBack=1;
for(int i=0;i<s.size();i++) { if(isFront){
if(!isalnum(s[i])) {
s.erase(s.begin()+i);
i--;
}
else isFront=0;
}
}
for(int i=s.size()-1;i>=0;i--){
if(isBack){
if(!isalnum(s[i])){
s.erase(s.begin()+i);
}
else isBack=0;
}
}
} int main()
{
string s;
int c=0;
while(cin>>s){
for(int i=0;i<s.size();i++) {
s[i]=tolower(s[i]);
}
c++;
trims(s);
int i;string s1;
while(!s.empty()){
int len=s.size();
for(i=0;i<s.size();i++){
if(!isalnum(s[i])) break;
}
if(i!=len) {
s1=s.substr(0,i);
trims(s1);
s=s.substr(i);
trims(s);
}else{
s1=s;
}
if(!s1.empty())
m[s1]++;
if(i==len) break;
}
}
map<string,int>::iterator ii=m.begin();
int max=-1;
string key;
while(ii!=m.end()){
if(ii->second>max){
max=ii->second;
key=ii->first;
}else if(ii->second==max){
if(key>ii->first){
key=ii->first;
}
}
ii++;
}
cout<<key<<" "<<max<<endl; return 0;
}

  

PAT1071. Speech Patterns (25)的更多相关文章

  1. Pat1071: Speech Patterns

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

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

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

  3. 1071. Speech Patterns (25)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  4. 1071 Speech Patterns (25)(25 分)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  5. PAT Advanced 1071 Speech Patterns (25 分)

    People often have a preference among synonyms of the same word. For example, some may prefer "t ...

  6. PAT (Advanced Level) 1071. Speech Patterns (25)

    简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...

  7. PAT甲题题解-1071. Speech Patterns (25)-找出现最多的单词

    分割字符串的用法+map映射给出input中出现次数最多的单词,如果次数相同,给出按字典序最小的. 这里我用了自定义分隔符来读取字符串,方法如下: //按照定义的分隔符d来分割字符串,对str进行读取 ...

  8. A1071 Speech Patterns (25 分)

    一.技术总结 开始拿到这道题目时,思考的是我该如何区分它们每一个单词,不知道这里还是要学习得知在cctype头文件中有一个函数用于查看是否为0~9.a~z.A~Z,就是isalnum(),又因为题目中 ...

  9. 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))

    题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...

随机推荐

  1. PHP file_get_contents和curl区别

    一.file_get_contents 1.定义 file_get_contents() 函数将指定 URL 的文件读入一个字符串并返回. 2.语法 file_get_contents(path, i ...

  2. 006-Shell printf 命令

    一.概述 printf 命令模仿 C 程序库(library)里的 printf() 程序. printf 由 POSIX 标准所定义,因此使用 printf 的脚本比使用 echo 移植性好. pr ...

  3. git获取远程仓库代码

    首先在本地创建一个目录“ MyProject”,用来存放工程文件,git进入该文件夹,执行 git clone 远程项目MyCode地址 将代码克隆到本地然后进入“MyCode”文件夹下 cd MyC ...

  4. 【zabbix】自定义监控项key值

    说明: zabbix自带的默认模版里包括了很多监控项,有时候为了满足业务需求,需要根据自己的监控项目自定义监控项,这里介绍一种自定义监控项的方式. 1,首先编写自定义监控脚本,本文以监控httpd进程 ...

  5. 解决Centos关闭You have new mail in /var/spool/mail/root提示(转)

    今天查看内存的时候 出现一天奇怪的提示 You have new mail in /var/spool/mail/root 有的时候每敲一下回车,就出来You have new mail in /va ...

  6. Selenium IDE界面学习

  7. Selenium+Python学习之一

    刚入门selenium+Python,实验成功之后,记录一下过程. 首先是在知乎上面看到一个关于selenium+python的示例,于是自己便尝试搭建环境上手实验. 按照作者的代码敲一遍之后执行,竟 ...

  8. python3 对excel读、写、修改的操作

    一.对excel的写操作实例: 将一个列表的数据写入excel, 第一行是标题,下面行数具体的数据 import xlwt #只能写不能读 stus = [['姓名', '年龄', '性别', '分数 ...

  9. Java基础知识陷阱(七)

    本文发表于本人博客. 上次说了下HashSet和HashMap之间的关系,其中HashMap这个内部有这么一句: static final float DEFAULT_LOAD_FACTOR = 0. ...

  10. 从两道题看go channel的用法

    在知乎看到有人分享了几道笔试题,自己总结了一下其中与channel有关的题目.全部题目在这里: https://zhuanlan.zhihu.com/p/35058068 题目 5.请找出下面代码的问 ...