需要解释的是本题此处是因为如果输入的有效数值是在末尾的,那么此处应该进行一次特判;
if(i==str.size()-1)
{
if(a.find(temp)!=a.end())a[temp]++;
else a[temp]=1;
}
#include<cstdio>
#include<iostream>
#include<string>
#include<algorithm>
#include<map>
using namespace std;
map <string,int> a;
bool check(char x)
{
if(x>='0'&&x<='9')return true;
else if(x>='a'&&x<='z')return true;
else if(x>='A'&&x<='Z')return true;
else return false;
}
int main()
{
string str, temp;;
getline(cin,str);
for (int i=0;i<str.size();i++)
{
if(check(str[i]))
{
if(str[i]>='A'&&str[i]<='Z')
str[i]+=32;
temp+=str[i];
if(i==str.size()-1)
{
if(a.find(temp)!=a.end())a[temp]++;
else a[temp]=1;
}
}
else
{
if(temp!="")
{
if(a.find(temp)!=a.end())a[temp]++;
else a[temp]=1;
temp.clear();
}
}
}
int max=0;
string ans;
for (map<string,int>::iterator it=a.begin();it!=a.end();it++)
{
if(it->second > max)
{
max=it->second;
ans=it->first;
}
}
cout<<ans<<" "<<max;
}
本文介绍了一种使用C++处理字符串的方法,通过遍历字符串并统计有效字符出现的次数,找出出现频率最高的字符串及其频率。涉及C++标准库中的字符串处理、条件判断和映射容器的使用。
508

被折叠的 条评论
为什么被折叠?



