PAT Advanced A1071 Speech Patterns (25) [map映射,STL的使⽤]

题目

People ofen 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 ‘ ’. 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 输入整行,取出单个字符串,转小写,累计出现次数

因为接收再切割比较麻烦,所以变换一种思路:遍历每个字符,如果是字母或数字统计到buffer中,如果不是字母或数字,就说明已经切割出一个字符串,累计其出现次数,并清空buffer

2 map统计字符串出现次数,max记录出现次数最多的字符串
3 打印max即其出现次数

Code

#include <iostream>
#include <unordered_map>
#include <string>
const int INF=0x7fffffff;
using namespace std;
unordered_map<string,int> mp;
void strlow(string &str) {
	for(int i=0; i<str.length(); i++)
		str[i]=tolower(str[i]);
}
int main(int argc,char * argv[]) {
	string str,buf,max;
	getline(cin,str);
	for(int i=0; i<str.length(); i++) {
		char &cr = str[i];
		if(isalnum(cr)) {
			cr=tolower(cr);
			buf+=cr;
		}
		if((!isalnum(cr)||i==str.length()-1)&&buf.length()>0) {
			// 统计buffer中的字符串,并情况buffer
			mp[buf]++;
			if (mp[max]<mp[buf]||(mp[max]==mp[buf]&&max>buf)) 
				max=buf;
			buf="";
		}
	}
	printf("%s %d",max.c_str(),mp[max]);
	return 0;
}

原文地址:https://www.cnblogs.com/houzm/p/12483659.html