数据结构day22

总结:准备写排序的,题目推荐先学一下map,所以下面是map的题目

题目详情 - 1044 火星数字 (pintia.cn)

思路: 因为最大168,所以原本想着用if--else分两位数和一位数输出即可;然后看了一眼答案,用的打表,我没试过打表,所以跟着答案写了一次;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<cctype>
#include<string>
#pragma warning(disable:4996)
using namespace std;

int main() {
	//store data
	string int_to_str[13]={ "tret","jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};//1-12
	string int_to_str_carry[13]={"tret", "tam", "hel", "maa","huh" , "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};//carry
	string num_to_str[170];//打表
	map<string, int>str_to_num;
	//打表操作
	for (int i = 0; i < 13; i++) {
		num_to_str[i] = int_to_str[i];//个位,十位为0
		num_to_str[i * 13] = int_to_str_carry[i];//十位,个位后面打表
		str_to_num[int_to_str[i]] = i;//个位
		str_to_num[int_to_str_carry[i]] = i * 13;//只有十位,个位0
	}
	for (int i = 1; i < 13; i++) {//十位和个位都不为0
		for (int j = 1; j < 13; j++) {
			string str = int_to_str_carry[i] + ' ' + int_to_str[j];
			num_to_str[i * 13 + j] = str;
			str_to_num[str] = i * 13 + j;
		}
	}
	//print
	int n;
	cin >> n;
	cin.get();//blank
	string s;
	while (n--) {
		getline(cin, s);
		if (isdigit(s[0])) {//是数字
			int num = 0;
			for (int i = 0; i < s.size(); i++) {
				num = num * 10 + (s[i] - '0');
			}
			cout << num_to_str[num] << endl;
		}
		else//输入是字符
		{
			cout << str_to_num[s]<<endl;
		}
	}
	return 0;
}

题目详情 - 1054 The Dominant Color (pintia.cn)

思路:原本以为是大整数问题,用到map,结果仔细一看2^24<2^31-1,但是用map可以省很多空间;所以记录每个数字出现的次数,然后遍历map找到最大那个即可;

值得一提的是mp.find()返回迭代器;

还值得一提的是答案给出不用map的思路,即出现不同的int就抵消,最后一个就是出现次数最多的int;明天写一写;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<cctype>
#include<string>
#pragma warning(disable:4996)
using namespace std;

int main() {
	int m, n;
	cin >> m >> n;
	map<int, int>mp;//2^24<2^31-1
	int temp;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < m; j++) {
			cin >> temp;
			if (mp.find(temp)!=mp.end()) {
				mp[temp]++;
			}
			else
			{
				mp[temp] = 1;
			}
		}
	}
	int k = 0, max = 0;
	for (map<int, int>::iterator it = mp.begin(); it != mp.end(); it++) {
		if (max < it->second) {
			max = it->second;
			k = it->first;
		}
	}
	cout << k;
	return 0;
}

题目详情 - 1071 Speech Patterns (pintia.cn) 

思路:注意string.empty()用法是判断str是否为空,而想要清空字符串应该使用str.clear() ;第一次写测试点4卡着了,万万没想到牛客网的过了;原来是只输入一个字符(比如a)时,出现问题,因为一开始我是while(c=cin.get()&&c!='\n'),这样直接跳出了循环,把判断是否为换行符换到后面就解决了问题;

#include<iostream>
#include<algorithm>
#include<string.h>
#include<map>
#include<cctype>
#include<string>
#pragma warning(disable:4996)
using namespace std;

int main() {
	string str;
	char c;
	map<string, int>mp;
	while (c=cin.get()) {
		if (isalnum(c)) {//全部小写
			if (isalpha(c)) {
				c = tolower(c);//-32
			}
			str += c;
		}
		else
		{
			if (mp.find(str) != mp.end()) {
				mp[str]++;
			}
			else
			{
				mp[str] = 1;
			}
			str.clear();
			if (c == '\n') {
				break;
			}
		}
	}
	//字典序,map自动排序,所以只用找第一个最大值
	int max = 0;
	string temp;
	for (map<string, int>::iterator it = mp.begin(); it != mp.end(); it++) {
		if (it->first == "") {//不要空字符串
			continue;
		}
		if (it->second > max) {
			temp = it->first;
			max = it->second;
		}
	}
	cout << temp <<' ' << max;

	return 0;
}

题目详情 - 1022 Digital Library (pintia.cn) 

思路:原本想着用map查找元素只有logN,这样就在10^7之下不会超过时间限制,写完发现报错了,如下:
错误    C2676    二进制“<”:“const _Ty”不定义该运算符或到预定义运算符可接收的类型的转换 

STFW知道,map是按照key排序的,我设置了map<node,string>所以需要从写map的cmp,可惜了我对原理不了解,然后一看答案,还用了set,这不,还没学set,所以明天学set,学完写着题,然后写排序剩下那题,可以说是开始递归了;

值得一提的是,string.find()当查找不到时返回string::npos是字符串可储存的最大字符数,通常是无符号int或无符号long的最大取值 ,我的VS2022是无符号long最大值,cout << ULLONG_MAX << endl 可以看出;
 

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值