PATA 1071 Speech Patterns.

#include <bits/stdc++.h>
using namespace std;
bool check(char c)//检查是否为字母或数字
{
	if(c>=‘A‘&&c<=‘Z‘||c>=‘a‘&&c<=‘z‘||c>=‘0‘&&c<=‘9‘)
		return true;
	else
		return false;
}
int main()
{
	map<string,int> count;
	string str;
	getline(cin,str);
	int i = 0;
	while(i<str.length())
	{
		string word;
		while(i<str.length()&&check(str[i]))
		{
			if(str[i]>=‘A‘&&str[i]<=‘Z‘){  //大写统一改为小写
				str[i] += (‘a‘-‘A‘);
			}
			word += str[i]; //字符拼接 +
			i++;
		}
		if(word != ""){
			if(count.find(word)==count.end())	count[word]=1;  //这单词还没出现过,计数为1
			else count[word]++;
		}
		while(i<str.length()&&!check(str[i]))  //遇到其它字符跳过
		{
			i++;
		}
	}
	string ans;
	int  MAX=0;
	for(map<string,int>::iterator it=count.begin();it != count.end();it++)  //只能用!=,而不能用<end()
	{
		if(it->second>MAX)
		{
			ans = it->first;
			MAX = it->second;
		}
	}
	cout<<ans<<" "<<MAX<<endl;
	return 0;

}

  

原文地址:https://www.cnblogs.com/qiangz/p/8552382.html

时间: 2024-08-25 04:09:09

PATA 1071 Speech Patterns.的相关文章

PAT 1071. Speech Patterns (25)

1071. Speech Patterns (25) 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 ident

PAT 1071 Speech Patterns[一般]

1071 Speech Patterns (25 分) 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 iden

1071. Speech Patterns (25)

map的使用 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming 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

PAT:1071. Speech Patterns (25) AC

#include<ctype.h> #include<iostream> #include<map> #include<string> using namespace std; bool check(char a) //判断是否为有效字符 { if(isdigit(a) || isalpha(a)) return true; return false; } int main() { map<string,int> count; //单词与次数的映

PAT 1071. Speech Patterns

又是考输入输出 #include <cstdio> #include <cstdlib> #include <string> #include <vector> #include <unordered_map> #include <algorithm> using namespace std; char buf[1048577]; bool is_alphanumerical(char &ch) { if (ch >=

PAT (Advanced Level) 1071. Speech Patterns (25)

简单题. #include<cstdio> #include<cstring> #include<cmath> #include<vector> #include<map> #include<stack> #include<queue> #include<string> #include<iostream> #include<algorithm> using namespace std;

1071 Speech Patterns

题意:给出一个字符串,找出词频最高的单词和相应的词频.(这个就是我之前做的一个项目的一个函数啊,哈哈哈) 思路:利用map定义字典map<string,int> dict.主要是字符串的截取,这里用上了几个比较方便的函数,总结如下: 几个有助于节省编码时间的字符串处理函数(在头文件<ctype.h>或<cctype>下) isalnum 判断字符是否为字母(含大小写)或数字 isalpha 判断字符是否为字母(含大小写) islower 判断字符是否为小写字母 isup

pat1071. Speech Patterns (25)

1071. Speech Patterns (25) 时间限制 300 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HOU, Qiming People often have a preference among synonyms of the same word. For example, some may prefer "the police", while others may prefer "the cops&qu

Speech Patterns (string)

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 v