ZeroJudge D517: Copying Text V1

From the machine, words consisting of four English letters keep appearing. Now, you need to transcribe them. If the word has appeared before, you'll write it directly using its assigned number. If the word has not appeared before, you'll 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)
The first line of each input set
There is a number N ( 1 ≦ N ≦ 105)
Following that, there will be N lines of words spoken by the mad scientist.
Each line consists of a 4-letter word composed of lowercase letters from 'a' to 'z'.
If the string has appeared before, output its assigned number; otherwise, output the number it will be assigned.
5
eine
isis
zwei
drei
zwei
New! 1
New! 2
New! 3
New! 4
Old! 3

Thought Process

Use a Map to record whether a string has appeared before. If Map[received string] == 0, it means the string has not appeared before. Declare a count variable and initialize it to 1, then set Map[received string] to count and increment count. If it is not equal to 0, it means the string has appeared before, so output Map[received string].

Sample Code-ZeroJudge D517: Copying Text V1

#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)
            {
                cout << "Old! " << MAP[str] << "\n";
                continue;
            }
            MAP[str] = count;
            count++;
            cout << "New! " << MAP[str] << "\n";
        }
    }
}

//ZeroJudge D517
//Dr. SeanXD

Comments