【PAT】A1071 Speech Patterns *

People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops". Analyzing such patterns can help to narrow down a speaker's identity, which is useful when validating, for example, whether it's still the same person behind an online avatar.

Now given a paragraph of text sampled from someone's speech, can you find the person's most commonly used word?

Input Specification:

Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return \n. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].

Output Specification:

For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.

Sample Input:

Can1: "Can a can can a can?  It can!"

Sample Output:

can 5

 解题思路:

  1. 理解题意:给一字符串,找出里面最多的单词,输出单词和出现的次数。因为map在比较过程中
  2. 代码分为3段:首先是得到字符串,可以用iostream下的getline(cin,stringname);来获得,可以好处是无视空格的中断;(在这里比较三种获得字符或字符串的办法,第一种见上;第二种是gets(stringname);火来获得字符串,但是出现空格会导致输入失败,也就是说,不出现空格的字符串可以用gets();第三种是获得字符,getchar(),这种获得方式一般用来吃掉空格);第二段,处理大写英文字符变为小写,同时得到单词,一得到单词就检索整个mp看有没有和他一样的,有就甲乙,没有就置1,如果不是单词的组成部分,就计数器+1,直到第i个字符是单词的组成元素;第三段,输出,注意map映射的输出方式;
  3. 总结:本来这道题是不难的,但是我还是看了笔记才能准确的做出来,吸取的教训如下:①map中的key是通过红黑树排序过的,key都是从小到大的,所以本题只要排好value大小就可以了;②单词非空,那么+1;我本来写的是mp[str]>0才+1,后面到PTA里run一下也是正确的,这里就不吐槽自己了;③对非单词组成,原先没有考虑周到,直接跳过但是没有对计数器做+1操作,这是逻辑上自己没有理顺;④我觉得最不好的一点就是没有对是否是单词组成部分做一个预先的判断函数,导致代码很冗长。⑤最后最大的bug是出现在括号/ku在修改的时候马虎了,导致在devc上跑不出来,后来对着宝典查出来了。本题还是比较有价值的,要吸取经验教训。

代码:

#include <map>
#include <string>
#include <iostream>
using namespace std;

bool check(char a){
	if(a>='0'&&a<='9') return true;
	if(a>='a'&&a<='z') return true;
	if(a>='A'&&a<='Z') return true;
	return false;
}

int main(){
	map<string,int> mp;
	string str;
	getline(cin,str);
	int len=str.length();
	int i=0;
	while(i<len){
		string str_t;
		while(i<len&&check(str[i])==true){  //
			if(str[i]>='A'&&str[i]<='Z'){
				str[i]=str[i]+32;
			}
			str_t += str[i];  //+= *= /=
			i++;
		}
		if(str_t!=""){
			if(mp.find(str_t)==mp.end()) mp[str_t]=1;
			else mp[str_t]++;
		}
        /*
	    if(mp[str_t]>0) mp[str_t]++;
		else mp[str_t]=1;
        */
		while(i<len&&check(str[i])==false){
			i++;
		}
	}
	int max=0;
	string str_y;
	for(map<string,int>::iterator it=mp.begin();it!=mp.end();it++){
		if(it->second>max){
			max=it->second;
			str_y=it->first;
		}
	}
	cout<<str_y<<" "<<max<<endl;
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

_之桐_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值