ZeroJudge D518: Copying Text

From the machine, words of various lengths continuously appear, represented by "?". Your task is to transcribe these words. If the word has appeared before, you will write it using its assigned number. If the word has not appeared before, you will assign it a new number.

For each new set of data, that represents a different event, please do not assign it a new number.

Sample Inputs/Outputs

Sample Input(s)Sample Output(s)
EOF inputs: the first line of each set of inputs contains a number N (1 ≤ N ≤ 10^4).
Following that, there will be N lines of words spoken by the mad scientist.
Each line consists of a word of "?" characters, constructed from lowercase letters 'a' to 'z' (1 ≤ ? ≤ 20).
If the string has appeared before, output its assigned number; otherwise, output the number it will be assigned.
5
eine
isis
zwei
drei
zwei
6
abc
abcd
abdc
aabb
bca
abc
8
aaa
aab
aaa
aac
aaaabbbsc
caa
aaba
aab
New! 1
New! 2
New! 3
New! 4
Old! 3
New! 1
New! 2
New! 3
New! 4
New! 5
Old! 1
New! 1
New! 2
Old! 1
New! 3
New! 4
New! 5
New! 6
Old! 2

Thought Process

Use a Map to record whether each string has appeared before. Initialize a variable to 1 to store the number for each new string. If the string is new, set its Map value to the variable, and then increment this variable by 1. If it is an old string, output the Map value for that old string.

Sample Code-ZeroJudge D518: Copying Text

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

int main() {
    cin.sync_with_stdio(0);
    cin.tie(0);
    int N;
    while (cin >> N)
    {
        map<string, int>MAP;
        int count = 1;
        for (int i = 0; i<N; i++)
        {
            string str;
            cin >> str;
            if (MAP[str] == 0)
            {
                MAP[str] = count;
                cout << "New! " << count << "\n";
                count++;
            }
            else cout << "Old! " << MAP[str] << "\n";
        }
    }
}

//ZeroJudge D518
//Dr. SeanXD

Comments