ZeroJudge A541: Dictionary

 7000 Vocabularies

The nightmares of all high school students.

But for smart Yu-Chun, it's a different story.

He claims to have 70,000 vocabularies memorized. 

He sneers at words he has already memorized.

Because it makes him feel very bored.

But there will always be words in the world that he hasn't seen before, right?

Not a problem.

He can remember them at a glance.

Fortunately,

God treats everyone fairly.

Yu-Chun doesn't know how to code.

He always finds it troublesome to determine whether he has memorized these words or not...

We are intelligent people.

Let's help Yu-Chun by writing a program! 

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
Each test case consists of only one dataset. The first line contains an integer N (1 < N < 70000), representing the number of words known by Yu-Chun. The next 2 to N+1 lines each contain a non-repeating English word with a length of less than 20 characters. All words consist of lowercase English characters only. The N+2 line contains a positive integer Q (1 < Q < 70000). The next Q lines each contain a word to be queried. All words consist of lowercase English characters only and have a length of less than 20 characters.For each queried word, output the query result on a separate line. If Yu-Chun has already memorized the word, output "yes". If Yu-Chun has not yet memorized the word, output "no" and add this word to Yu-Chun's memory.
4
henry
john
mary
cindy
5
andy
cindy
vicky
leo
leo
no
yes
no
no
yes

Thought Process

Use a Map to determine whether each string has appeared before. It is important to note that when evaluating Q strings, there may be cases where the same string appears multiple times. In such cases, even if the string does not appear in the N strings, "yes" should be output because it has already appeared before.

Sample Code-ZeroJudge A541: Dictionary

#include <iostream>
#include <map>
using namespace std;

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    cin >> N;
    map<string, int>MAP;
    for (int i = 0; i<N; i++)
    {
        string str;
        cin >> str;
        MAP[str]++;
    }
    int Q;
    cin >> Q;
    for (int i = 0; i<Q; i++)
    {
        string str;
        cin >> str;
        if (MAP[str] == 0) cout << "no\n";
        else cout << "yes\n";
        MAP[str]++;
    }
}

//ZeroJudge A541
//Dr. SeanXD

Comments