A1071 Speech Patterns (25) map的常见用法

本文介绍了一种使用C++处理字符串的方法,通过遍历字符串并统计有效字符出现的次数,找出出现频率最高的字符串及其频率。涉及C++标准库中的字符串处理、条件判断和映射容器的使用。

需要解释的是本题此处是因为如果输入的有效数值是在末尾的,那么此处应该进行一次特判;

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;
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值