PAT1071 Speech Patterns 哈希表字符串模拟

Speech Patterns/说话方式

题目大意:

不同的人对描述同一种事物的同义词的偏爱程度可能不同。
例如,在说警察时,有人喜欢用 the police,有人喜欢用 the cops。
分析说话方式有助于确定说话者的身份,这在验证诸如和你线上聊天的是否是同一个人十分有用。
现在,给定一段从某人讲话中提取的文字,你能确定他的最常用词吗?

输入格式
输入共一行,包含一个字符串,以回车符 \n 终止。

输出格式
共一行,输出最常用词以及其出现次数。

如果常用词有多个,则输出字典序最小的那个单词。

单词不区分大小写。

数据范围
输入字符串长度不超过 1048576,且至少包含一个大小写字母或数字。

输入样例:
Can1: “Can a can can a can? It can!”
输出样例:
can 5

解题思路:
一道水题,一开始先用getline读入一行的字符串,然后建立一个unorder_map哈希表,然后逐步查找合法的单词,注意要转成小写(tolower()),最后遍历哈希表找到出现次数最多且字典序最小的即可。

Code;

#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<unordered_map>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int inf=0x3f3f3f3f;
const int maxn=1e5+7;

bool check(char x){            //判断是否合法
	if(x>='0'&&x<='9') return true;
	if(x>='a'&&x<='z') return true;
	if(x>='A'&&x<='Z') return true;
	return false;
}

int main()
{
    string str;
    getline(cin,str);                     //读入
    
    unordered_map<string,int> hash;    //建立哈希表
    
    for(int i=0;i<str.size();i++){
    	if(check(str[i])){                  //查找合法字符
    		string word;
    		int j=i;
    		while(j<str.size()&&check(str[j])) word+=tolower(str[j++]);
			
			hash[word]++;
			i=j; 
		}
	} 
	
	int maxx=-1;
	string ans;
	for(auto it:hash){                     //遍历哈希表
		if(it.second>maxx||it.second==maxx&&it.first<ans){
			ans=it.first;
			maxx=it.second;
		}
	}
	cout<<ans<<" "<<maxx<<"\n";
    return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_43872264/article/details/107841276