# The tragedy of test algorithm training

Keywords: Algorithm STL

## The tragedy of test algorithm training

subject

Resource constraints

Time limit: 1.0s memory limit: 512.0MB

Problem description

English preparation gzp is a funny (tu) than (hao). In order not to fail in the upcoming English quiz, gzp forgets to eat and sleep and reviews the English appendix word list, just like a human tragedy. But God has the virtue of living a good life. God threw gzp a piece of paper on which the words to be tested were recorded. But gzp is funny. He forgot all the things he reviewed before, so he has to review again. However, you already know the words to be tested, so you don't need to review all the pages of the word list. Therefore, now you need to help him find out how many pages need to be reviewed. He will tell you on which pages each word will appear, and tell you which words to test. You just tell him the answer. Since a word will appear on different pages, you only need to review what is on the front page.

Input format

An integer n in the first line indicates that there are n words in the word appendix. In the next N lines, each line consists of a word with lowercase letters and an integer, indicating a word and the number of pages it is on. Next is a line of integer m, indicating m words to be tested, and next M lines of words composed of lowercase letters, indicating the words to be tested.

Output format

A number indicating the number of pages to review.

sample input

5
ab 1
ac 2
ab 2
ac 3
c 3
3
ab
ac
c

sample output

3

Data scale and agreement

0 < = n, m < = 100000, word length < = 10.

It's easy to think of using map

It is worth noting that since a word will appear on different pages, you only need to review the one on the front page., This sentence means that if the same word appears many times, only review the one on the front page, such as ab 2, ab 3 and ab 1. If you want to review AB, just review the first page.

1.map code

Idea: for each word, we only save the top pages

```#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
//Change a short one
typedef map<string, int>::iterator MyIt;

int main()
{
//word
map<string, int> words;
//Number of pages to review
set<int> pages;
int n;
cin >> n;
for ( int i = 0; i< n; ++i ) {
string s;
int num;
cin >> s >> num;//Enter words and pages
if ( words.count(s) == 0 ) { //If not in words, just insert it
words[s] = num;
}
else {//The word s has already appeared,
MyIt it = words.find(s);
/*
We only save the top of the page!!!!
*/
if ( it->second > num ) {
words[s] = num;
}
}
}
int m;
cin >> m;
for ( int i = 0; i < m; ++i ) {
string s;
cin >> s;
MyIt it = words.find(s);
pages.insert(it->second);
}
cout << pages.size() << endl;
return 0;
}

```

multimap to do:

This allows repeated keys to appear, that is, keys can appear repeatedly in multimap, and the repeated keys are adjacent, but she does not support the [] operator, so insert is used

```#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef multimap<string, int>::iterator MyIt;
int main()
{
multimap<string,int> words;
words.insert(make_pair("ab",1));
words.insert(make_pair("cc",1));
words.insert(make_pair("dd",1));
words.insert(make_pair("cc",1));
words.insert(make_pair("ab",1));
for ( auto temp : words ) {
cout << temp.first << "->" << temp.second << endl;
}
return 0;
}
```

And because find finds the first occurrence, you can traverse to find the minimum number of pages where words appear.

```#include <iostream>
#include <map>
#include <set>
#include <string>
using namespace std;
typedef multimap<string, int>::iterator MyIt;

int main()
{
multimap<string, int> words;
set<int> pages;
int n;
cin >> n;
//Save it all
for ( int i = 0; i< n; ++i ) {
string s;
int p;
cin >> s >> p;
words.insert(make_pair(s,p));
}

int m;
cin >> m;
for ( int i = 1; i <= m; ++i ) {
string s;
cin >> s;
MyIt it = words.find(s);
//k indicates the number of times s occurs!!!!!
int k = words.count(s), front = it->second;
it++;
//Traverse to find the minimum number of pages where words appear
for ( int v = 1; v < k; v++, it++ ) {
if ( it->second < front ) {
front = it->second;
}
}
pages.insert(front);
}

cout << pages.size() << endl;
return 0;
}

```

Posted by joyser on Thu, 11 Nov 2021 15:17:33 -0800