1071. Speech Patterns (25)

1.首先是字符串处理,把大写字母的全改为小写字母

2.字符串分隔,遇到不是数字或者字母的字符,就进行分隔

3.使用map来存储次数,最后再存到vector中排序输出

4.需要使用scanf读取字符,不能使用getline




AC代码:

//#include<string>
//#include<stack>
//#include<unordered_set>
//#include <sstream>
//#include "func.h"
//#include <list>
#include <iomanip>
#include<unordered_map>
#include<set>
#include<queue>
#include<map>
#include<vector>
#include <algorithm>
#include<stdio.h>
#include<iostream>
#include<string>
#include<memory.h>
#include<limits.h>
#include<stack>
using namespace std;
/*
Here a "word is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end

*/
bool cmp(const pair<string, int>&a,const pair<string, int>&b)
{
	if (a.second > b.second) return true;
	else if (a.second == b.second && a.first < b.first) return true;
	else return false;
}
int main(void)
{
	string s="";
	char c = '1';
	while (c!='\n')
	{
		scanf("%c", &c);
		s += c;
	}
	
	map<string, int> times;
	string word = "";
	for (int i = 0; i < s.size(); i++)
	{
		if ((s[i] >= 'a'&&s[i] <= 'z') || (s[i] >= 'A'&&s[i] <= 'Z') || (s[i] >= '0'&&s[i] <= '9'))
		{//是字母
			if ((s[i] >= 'A'&&s[i] <= 'Z'))
				s[i] = s[i] - 'A' + 'a';
			word += s[i];
		}
		else
		{//不是字母,则清空word
			if (word != "")
				times[word]++;
			word = "";
		}
	}

	vector<pair<string, int>> ans(0);
	for (map<string, int>::iterator ite = times.begin(); ite != times.end(); ite++)
	{
		ans.push_back( *ite);
	}
	sort(ans.begin(), ans.end(), cmp);

	cout << ans[0].first << " " << ans[0].second << endl;

	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值