1. 程式人生 > 其它 >2021-7-20 Registration system

2021-7-20 Registration system

難度 1300

題目 CodeForces:

C. Registration system time limit per test                                             5 seconds memory limit per test 64 megabytes

  A new e-mail service "Berlandesk" is going to be opened in Berland in the near future. The site administration wants to launch their project as soon as possible, that's why they ask you to help. You're suggested to implement the prototype of site registration system. The system should work on the following principle.

  Each time a new user wants to register, he sends to the system a request with hisname. If such anamedoes not exist in the system database, it is inserted into the database, and the user gets the responseOK, confirming the successful registration. If thenamealready exists in the system database, the system makes up a new user name, sends it to the user as a prompt andalso inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another toname(name1,name2, ...), among these numbers the leasti

is found so thatnameidoes not yet exist in the database.

Input

  The first line contains numbern(1 ≤ n ≤ 105). The followingnlines contain the requests to the system. Each request is a non-empty line, and consists of not more than 32 characters, which are all lowercase Latin letters.

Output

  Printn

lines, which are system responses to the requests:OKin case of successful registration, or a prompt with a new name, if the requested name is already taken.

Keyword

site administration 網站管理

implement 執行,實現,實施

prototype 原型

database 資料庫

prompt 提示

題目解析

本題大意是有n個使用者進行註冊,先註冊的使用的名字會被記錄進資料庫中,視為成功註冊,後註冊同樣名字的使用者,會以原先名字後面增加數字以後被記錄進資料庫中,同樣也視為成功註冊。

本題思路很簡單,由於註冊的使用者只會用小寫字母註冊,也就意味著事實上重複使用者無需記錄新名字,只需要記錄有多少個重複使用者即可

這裡我們利用map來存每個名字出現的次數,如果沒有出現過,我們就輸出"OK"並將這個名字放入資料庫,並將這個名字對應的value記為0,如果出現過,我們就輸出原先的名字加上該名字對應的value+1,然後將value這個值加上1.

解析完畢,以下為參考程式碼

 1 #include<iostream>
 2 #include<map>
 3 #include<string>
 4 using namespace std;
 5 int main()
 6 {
 7     int n; cin >> n;
 8     map<string,int>system;
 9     for (int i = 0; i < n; i++)
10     {
11         string m;cin >> m;
12         if (system.find(m) != system.end())
13         {
14             system[m]++;
15             cout << m << system[m] << "\n";
16         }
17         else
18         {
19             cout << "OK\n";
20             system[m] = 0;
21         }
22     }
23     return 0;
24 }