题目要求的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. 算法大神之路——排序

    从今天开始,给自己立下一个目标,每天晚上写一篇算法与数据结构的博客,用来给自己以后的算法工程师的目标铺路! 今天晚上就以算法里面的排序,作为自己的第一章节吧. 排序,就是讲一组数据,按照特定的规则去调 ...

  2. spring data jpa 遇到的问题

    org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.O ...

  3. CentOS7系统基本操作

    查看网卡命令 [root@localhost ~]# ip addr 1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state ...

  4. Android实现按两次back键退出应用

    重写onKeyDown()方法 System.currentTimeMillis():该方法的作用是返回当前的计算机时间,时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0 ...

  5. MySQL优化(三):优化数据库对象

    二.优化数据库对象 1.优化表的数据类型 应用设计的时候需要考虑字段的长度留有一定的冗余,但不推荐很多字段都留有大量的冗余,这样既浪费磁盘空间,也在应用操作时浪费物理内存. 在MySQL中,可以使用函 ...

  6. CF519 ABCD D. A and B and Interesting Substrings(map,好题)

    A:http://codeforces.com/problemset/problem/519/A 水题没什么好说的. #include <iostream> #include <st ...

  7. Part01、sqlalchemy 使用

    一.ORM         连表               一对多               1.创建表,主动指定外键约束.               2.操作.                 ...

  8. 设置eclipse编码格式

    1.修改eclipse默认工作空间编码方式.点击Window-->Preferences-->General-->Workspace,设置编码格式为UTF-8,然后点击OK.

  9. PHP SQL写法 积累(注:PHPSQL与LINQ SQL相似)

    1: $data ['parentid'] = $pid; M('menu')->where($data)->order(' id asc  ')-> select();   // ...

  10. Python笔记 #01# Convert Python values into any type

    源:DataCamp datacamp 的 DAILY PRACTICE  + 日常收集. How much is your $100 worth after 7 years? Guess the t ...