2021-7-20 Registration system

时间:2021-07-20
本文章向大家介绍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 his name. If such a name does not exist in the system database, it is inserted into the database, and the user gets the response OK, confirming the successful registration. If the name already exists in the system database, the system makes up a new user name, sends it to the user as a prompt and also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, are appended one after another to name (name1, name2, ...), among these numbers the least i is found so that namei does not yet exist in the database.

Input

  The first line contains number n (1 ≤ n ≤ 105). The following n lines 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

  Print n lines, which are system responses to the requests: OK in 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 }

原文地址:https://www.cnblogs.com/lovetianyi/p/15037068.html