PAT练习笔记——6.4 map的常见用法详解

2019年9月PAT - 练习笔记——6.4

以下页码标注的是阅读器中实际页码,而不是书本身自印的页码。

第6章 C++标准模板库(STL)介绍

6.4 map的常见用法详解

目录

  1. B1044 / A1100 火星数字
  2. A1054 The Dominant Color
  3. A1071 Speech Pattern
  4. A1022 Digital Library

  1. B1044 / A1100 火星数字

    火星人是以 13 进制计数的:

    • 地球人的 0 被火星人称为 tret。
    • 地球人数字 1 到 12 的火星文分别为:jan, feb, mar, apr, may, jun, jly, aug, sep, oct, nov, dec。
      • 火星人将进位以后的 12 个高位数字分别称为:tam, hel, maa, huh, tou, kes, hei, elo, syy, lok, mer, jou。

    例如地球人的数字 29 翻译成火星文就是 hel mar;而火星文 elo nov 对应地球数字 115。为了方便交流,请你编写程序实现地球和火星数字之间的互译。

    输入格式:

    输入第一行给出一个正整数 N(<100),随后 N 行,每行给出一个 [0, 169) 区间内的数字 —— 或者是地球文,或者是火星文。

    输出格式:

    对应输入的每一行,在一行中输出翻译后的另一种语言的数字。

    输入样例:
    4
    29
    5
    elo nov
    tam
    
    输出样例:
    hel mar
    may
    115
    13
    
    1. 我的

      #include <iostream>
      #include <vector>
      #include <string>
      #include <map>
      
      using namespace std;
      
      const int MOD = 13;
      const vector<string> MP1 = {"tret", "jan", "feb", "mar", "apr", "may", "jun", "jly", "aug", "sep", "oct", "nov", "dec"};
      const vector<string> MP2 = {"xxx", "tam", "hel", "maa", "huh", "tou", "kes", "hei", "elo", "syy", "lok", "mer", "jou"};
      
      int main(void)
      {
      	map<string, int> MP3;
      	for (int i = 0;i < MP1.size();++i) MP3[MP1[i]] = i;
      	for (int i = 1;i < MP2.size();++i) MP3[MP2[i]] = i * MOD;
      	
      	int n = 0;
      	cin >> n;
      	getchar();
      	for (int i = 0;i < n;++i) {
      		char c = getchar();
      		ungetc(c, stdin);
      		
      		if ('0' <= c && c <= '9') {
      			int num = 0;
      			cin >> num;
      			getchar();
      			if (!num) cout << MP1[0];
      			else {
      				int high = num / MOD, low = num % MOD;
      				if (high) cout << MP2[high];
      				if (low) {
      					if (high) cout << " ";
      					cout << MP1[low];
      				}
      			}
      			cout << endl;
      		}
      		else {
      			string high = "", low = "";
      			getline(cin, low);
      			int pos = low.find(' ');
      			if (pos != string::npos) {
      				high = low.substr(0, pos);
      				low = low.substr(pos + 1, low.size() - pos - 1);
      				cout << MP3[high] + MP3[low] << endl;
      			}
      			else cout << MP3[low] << endl;;
      		}
      	}
      	
      	return 0;
      } 
      

      输出样例可以反过来作为输入样例

      测试点1:测试输入数字为0时的情况

    2. 《算法笔记》P252

      “直接针对给出的输入进行模拟会相对复杂,考虑到数据范围最多不超过168,因此不妨将[0, 168]的所有数都预处理出来(即打表),然后查询一个输出一个即可。这比直接通过输入来模拟要简单得多。”


  2. A1054 The Dominant Color

    Behind the scenes in the computer’s memory, color is always talked about as a series of 24 bits of information for each pixel. In an image, the color with the largest proportional area is called the dominant color. A strictly dominant color takes more than half of the total area. Now given an image of resolution M by N (for example, 800×600), you are supposed to point out the strictly dominant color.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains 2 positive numbers: M (≤800) and N (≤600) which are the resolutions of the image. Then N lines follow, each contains M digital colors in the range [0,224). It is guaranteed that the strictly dominant color exists for each input image. All the numbers in a line are separated by a space.

    Output Specification:

    For each test case, simply print the dominant color in a line.

    Sample Input:
    5 3
    0 0 255 16777215 24
    24 24 0 0 24
    24 0 24 24 24
    
    Sample Output:
    24
    
    1. 我的

      #include <iostream>
      #include <map>
      
      using namespace std;
      
      int main(void)
      {
      	int m = 0, n = 0, maximum = 0;
      	cin >> m >> n;
      	map<int, int> colors;
      	for (int i = 0;i < m;++i) {
      		for (int j = 0;j < n;++j) {
      			int color = 0;
      			cin >> color;
      			++colors[color];
      			if (colors[color] > colors[maximum]) maximum = color;
      		}
      	}
      	cout << maximum;
      	
      	return 0;
      } 
      
    2. 《算法笔记》P255


  3. A1071 Speech Pattern

    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?

    Input Specification:

    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].

    Output Specification:

    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.

    Sample Input:
    Can1: "Can a can can a can?  It can!"
    
    Sample Output:
    can 5
    
    1. 我的

      #include <iostream>
      #include <string>
      #include <map>
      
      using namespace std;
      
      bool IsAlpha(const char c)
      {
      	if (('0' <= c && c <= '9') || ('a' <= c && c <= 'z') || ('A' <= c && c <= 'Z')) return true;
      	return false;
      }
      
      int main(void)
      {
      	string input = "";
      	getline(cin, input);
      	
      	map<string, int> times;
      	string maximum = "";
      	for (int i = 0;i < input.size();) {
      		string word = "";
      		for (;i < input.size() && !IsAlpha(input[i]);++i);
      		for (;i < input.size() && IsAlpha(input[i]);++i) word += tolower(input[i]);
      		++times[word];
      		if ((times[word] > times[maximum]) || (times[word] == times[maximum] && word < maximum)) maximum = word;
      	}
      	cout << maximum << " " << times[maximum];
      	
      	return 0;
      }
      

      我不小心把(‘a’ <= c && c <= ‘z’)写成(‘z’ <= c && c <= ‘a’),dev c调试一直报段错误

      注意题目中给出的word的定义,不要想当然认为是英文单词

    2. 《算法笔记》P257


  4. A1022 Digital Library

    A Digital Library contains millions of books, stored according to their titles, authors, key words of their abstracts, publishers, and published years. Each book is assigned an unique 7-digit number as its ID. Given any query from a reader, you are supposed to output the resulting books, sorted in increasing order of their ID’s.

    Input Specification:

    Each input file contains one test case. For each case, the first line contains a positive integer N (≤104) which is the total number of books. Then N blocks follow, each contains the information of a book in 6 lines:

    • Line #1: the 7-digit ID number;
    • Line #2: the book title – a string of no more than 80 characters;
    • Line #3: the author – a string of no more than 80 characters;
    • Line #4: the key words – each word is a string of no more than 10 characters without any white space, and the keywords are separated by exactly one space;
    • Line #5: the publisher – a string of no more than 80 characters;
    • Line #6: the published year – a 4-digit number which is in the range [1000, 3000].

    It is assumed that each book belongs to one author only, and contains no more than 5 key words; there are no more than 1000 distinct key words in total; and there are no more than 1000 distinct publishers.

    After the book information, there is a line containing a positive integer M (≤1000) which is the number of user’s search queries. Then M lines follow, each in one of the formats shown below:

    • 1: a book title
    • 2: name of an author
    • 3: a key word
    • 4: name of a publisher
    • 5: a 4-digit number representing the year
    Output Specification:

    For each query, first print the original query in a line, then output the resulting book ID’s in increasing order, each occupying a line. If no book is found, print Not Found instead.

    Sample Input:
    3
    1111111
    The Testing Book
    Yue Chen
    test code debug sort keywords
    ZUCS Print
    2011
    3333333
    Another Testing Book
    Yue Chen
    test code sort keywords
    ZUCS Print2
    2012
    2222222
    The Testing Book
    CYLL
    keywords debug book
    ZUCS Print2
    2011
    6
    1: The Testing Book
    2: Yue Chen
    3: keywords
    4: ZUCS Print
    5: 2011
    3: blablabla
    
    Sample Output:
    1: The Testing Book
    1111111
    2222222
    2: Yue Chen
    1111111
    3333333
    3: keywords
    1111111
    2222222
    3333333
    4: ZUCS Print
    1111111
    5: 2011
    1111111
    2222222
    3: blablabla
    Not Found
    
    1. 我的

      #include <iostream>
      #include <string>
      #include <map>
      #include <queue>
      
      using namespace std;
      
      void Output(map<string, priority_queue<int, vector<int>, greater<int>>>mp, const string&query)
      {
      	if (mp[query].empty()) printf("Not Found\n");
      	for (;!mp[query].empty();mp[query].pop()) printf("%07d\n", mp[query].top());
      }
      
      int main (void)
      {
      	int n = 0;
      	cin >> n;
      	
      	map<string, priority_queue<int, vector<int>, greater<int>>> titles;
      	map<string, priority_queue<int, vector<int>, greater<int>>> authors;
      	map<string, priority_queue<int, vector<int>, greater<int>>> keys;
      	map<string, priority_queue<int, vector<int>, greater<int>>> publishers;
      	map<string, priority_queue<int, vector<int>, greater<int>>> years;
      	for (int i = 0;i < n;++i) {
      		int id = 0;
      		string title = "", author = "", publisher = "", year = "";
      		
      		cin >> id;
      		getchar();
      		getline(cin, title);
      		titles[title].push(id);
      		getline(cin, author);
      		authors[author].push(id);
      		char c = 0;
      		while (c != '\n') {
      			string key = "";
      			cin >> key;
      			keys[key].push(id);
      			c = getchar();
      		}
      		getline(cin, publisher);
      		publishers[publisher].push(id);
      		cin >> year;
      		years[year].push(id);
      	}
      	
      	int m = 0;
      	cin >> m;
      	for (int i = 0;i < m;++i) {
      		int number = 0;
      		cin >> number;
      		getchar();
      		getchar();
      		string query = "";
      		getline(cin, query);
      		cout << number << ": " << query << endl;
      		
      		switch(number) 
      		{
      		case 1:
      			Output(titles, query);
      			break;
      		case 2:
      			Output(authors, query);
      			break;
      		case 3:
      			Output(keys, query);
      			break;
      		case 4:
      			Output(publishers, query);
      			break;
      		case 5:{
      			Output(years, query);
      			break;
      		}
      		default:
      			cout << "ERROR" << endl;
      			break;	
      		}
      	}
      	
      	return 0;
      }
      

      测试点3、4:注意id的输出

    2. 《算法笔记》P261


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值