PAT1071. Speech Patterns (25)
题目要求的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)的更多相关文章
- Pat1071: Speech Patterns
1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming Peo ...
- PAT 甲级 1071 Speech Patterns (25 分)(map)
1071 Speech Patterns (25 分) People often have a preference among synonyms of the same word. For ex ...
- 1071. Speech Patterns (25)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- 1071 Speech Patterns (25)(25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT Advanced 1071 Speech Patterns (25 分)
People often have a preference among synonyms of the same word. For example, some may prefer "t ...
- PAT (Advanced Level) 1071. Speech Patterns (25)
简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #in ...
- PAT甲题题解-1071. Speech Patterns (25)-找出现最多的单词
分割字符串的用法+map映射给出input中出现次数最多的单词,如果次数相同,给出按字典序最小的. 这里我用了自定义分隔符来读取字符串,方法如下: //按照定义的分隔符d来分割字符串,对str进行读取 ...
- A1071 Speech Patterns (25 分)
一.技术总结 开始拿到这道题目时,思考的是我该如何区分它们每一个单词,不知道这里还是要学习得知在cctype头文件中有一个函数用于查看是否为0~9.a~z.A~Z,就是isalnum(),又因为题目中 ...
- 【PAT甲级】1071 Speech Patterns (25 分)(getline(cin,x))
题意: 输入一行字符串,输出出现过次数最多的由字母和数字组成的字符串以及它出现的次数(对大小写不敏感,输出全部输出小写). AAAAAccepted code: #define HAVE_STRUCT ...
随机推荐
- 【react读取文件】react发送GET请求读取静态文件
react中,使用发送请求的方式把static文件夹中的前端可访问的静态文件读取成字符串: 1.new request,需要用到getRequestHeaders组件 2.fetch获取respons ...
- centos7手动编译安装Libvirt常见问题
由于功能需要,体验了手动编译安装Libvrt,还是碰到了不少问题,这里总结如下仅限于centos7: 1.configure: error: You must install the pciacces ...
- 10张Gif动图让你弄懂递归等概念
图像(包括动图)是传递信息的一种高效方式,往往能增强表象.记忆与思维等方面的反应强度.所谓一图胜千言,说的就是这个道理. 今天为大家整理了十张动图GIFS,有助于认识循环.递归.二分检索等概念的具体运 ...
- Swift学习——Swift基础具体解释(一)
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/zhenyu5211314/article/details/34807025 注:由于基础部分在Swi ...
- Spotlight 连接SuSE11 linux报错的解决方法
1. 在客户端安装spotlight: 2.在SuSE11中建立新用户,并且安装了sysstat包: 3.使用spotlight连接服务器,连接时提示 errorcode:3114 reas ...
- 013-HQL中级3-Hive四种数据导入方式介绍
Hive的几种常见的数据导入方式这里介绍四种:(1).从本地文件系统中导入数据到Hive表:(2).从HDFS上导入数据到Hive表:(3).从别的表中查询出相应的数据并导入到Hive表中:(4).在 ...
- gui设计
芯艺,你好! ... ... ... 芯艺,再见! http://www.chipart.cn/ 附件 http://files.cnblogs.com/files/dong1/%E8%89%BA%E ...
- NAND flash阵营ToggleDDR和ONFI
NAND 闪存:目前闪存制造厂主要分为三星与东芝.海力士联合的ToggleDDR阵营和英特尔与美光为首的ONFI阵营 IM Flash Technologies(IMFT):由Intel和Micron ...
- 详解MySQL第三篇—DCL语句
DCL(Data Control Language)语句:数据控制语句,用于控制不同数据段直接的许可和访问级别的语句.这些语句定义了数据库.表.字段.用户的访问权限和安全级别.主要的语句关键字包括 g ...
- SQL Server 2008数据库手动提交的设置
有时候我们需要对SQL Server 2008数据库手动提交的方法进行设置,使用Oracle的朋友会注意到Oracle中的手工提交的,如果修改错了数据还可以Rollback.但在SQL Server ...