《算法笔记》6.3小节 6.4小节——C++标准模板库(STL)介绍->string map

⭐⭐⭐⭐PAT A1060 Are They Equal (25 分)
问题描述:If a machine can save only 3 significant digits, the float numbers 12300 and 12358.9 are considered equal since they are both saved as 0.123×10
5
with simple chopping. Now given the number of significant digits on a machine and two float numbers, you are supposed to tell if they are treated equal in that machine.

  • 输入
Each input file contains one test case which gives three numbers N, A and B, where N (<100) is the number of significant digits, and A and B are the two float numbers to be compared. Each float number is non-negative, no greater than 10 
100
 , and that its total digit number is less than 100.
  • 输出
For each test case, print in a line YES if the two numbers are treated equal, and then the number in the standard form 0.d[1]...d[N]*10^k (d[1]>0 unless the number is 0); or NO if they are not treated equal, and then the two numbers in their standard form. All the terms must be separated by a space, with no extra space at the end of a line.

Note: Simple chopping is assumed without rounding.
  • 样例输入1
3 12300 12358.9
  • 样例输出1
YES 0.123*10^5
  • 样例输入2
3 120 128
  • 样例输出2
NO 0.120*10^3 0.128*10^3
#define _CRT_SECURE_NO_WARNINGS 1
#include<cstdio>
#include<iostream>
#include<cstring>
#include<ctime>
#include<algorithm>
#include<cmath>
#include<vector>
#include<set>
#include<string>
using namespace std;
int n;//有效位数
string deal(string s, int & e)
{
	int k = 0;
	while (s.length() > 0 && s[0] == '0')
	{
		s.erase(s.begin());
	}
	if (s[0] == '.')
	{
		s.erase(s.begin());
		while (s.length() > 0 && s[0] == '0')
		{
			s.erase(s.begin());
			e--;
		}
	}
	else
	{
		while (k < s.length() && s[k] != '.')
		{
			k++;
			e++;
		}
		if (k < s.length())
			s.erase(s.begin() + k);
		if (s.length() == 0)
		{
			e = 0;
		}
		int num = 0;
		k = 0;
		string res;
		while (num < n)
		{
			if (k < s.length()) res += s[k++];
			else res += '0';
			num++;
		}
		return res;
	}
	
}
int main()
{
	string s1, s2, s3, s4;
	cin >> n >> s1 >> s2;
	int e1 = 0,e2 = 0;
	s3 = deal(s1, e1);
	s4 = deal(s2, e2);
	if (s3 == s4 && e1 == e2)
	{
		cout << "YES 0." << s3 << "*10^" << e1 << endl;
	}
	else
	{
		cout << "NO 0." << s3 << "*10^" << e1 << " 0." << s4 << "*10^" << e2 << endl;
	}
	return 0;
}

⭐⭐⭐⭐问题 A: 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 identity, which is useful when validating, for example, whether it’s still the same person behind an online avatar.

Now given a paragraph of text sampled from someone’s speech, can you find the person’s most commonly used word?

  • 输入
Each input file contains one test case. For each case, there is one line of text no more than 1048576 characters in length, terminated by a carriage return '\n'. The input contains at least one alphanumerical character, i.e., one character from the set [0-9 A-Z a-z].
  • 输出
For each test case, print in one line the most commonly occurring word in the input text, followed by a space and the number of times it has occurred in the input. If there are more than one such words, print the lexicographically smallest one. The word should be printed in all lower case. Here a "word" is defined as a continuous sequence of alphanumerical characters separated by non-alphanumerical characters or the line beginning/end.

Note that words are case insensitive.
  • 样例输入
Can1: "Can a can can a can?  It can!"
  • 样例输出
can 5

因为Map会自动按照字典序排序,所以不用考虑次数相同输出字典序最小的情况了。遇到第一个->second=max就输出即可。

#include <cstdio>
#include <string>
#include <iostream>
#include <map>
using namespace std;
int main()
{
	string a;
	map<string, int> mp;
	while (getline(cin, a))
	{
		string temp;
		int max = 1;
		for (int i = 0; i < a.length(); i++)
		{
			if (a[i] >= 'A' && a[i] <= 'Z')
				a[i] += 32;
			if ((a[i] >= '0' && a[i] <= '9') || (a[i] >= 'a' && a[i] <= 'z'))
				temp += a[i];
			else//当遇到空格时,temp存储了空格之前的单词
			{
				if (temp.length() != 0)
				{
					if (mp.find(temp) != mp.end())//可以找到temp的映射
					{
						int a = ++mp[temp];//temp的映射++
						if (a > max)//比较当前的temp与之前出现的最大次数的大小
							max = a;
					}
					else
						mp[temp] = 1;//如果没有找到,就让Temp的映射变为1,表示该单词出现过一次
					temp.clear();//清除temp中存储的单词,为下一个单词做准备
				}
			}
		}
		if (temp.length() != 0)
		{
			if (mp.find(temp) != mp.end())
			{
				int a = ++mp[temp];
				if (a > max)
					max = a;
			}
			else
				mp[temp] = 1;
			temp.clear();
		}
		for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++)
		{
			if (it->second == max)
			{
				printf("%s %d\n", it->first.c_str(), it->second);
				break;
			}
		}
		mp.clear();//mp清空,为下一段话做准备
	}
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值