[CF从零单排#22][hash / map]4C - Registration System

题目来源:http://codeforces.com/contest/4/problem/C

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 name i does not yet exist in the database.

Input
The first line contains number n (1 ≤ n ≤ 10^5). 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.

Examples
input
4
abacaba
acaba
abacaba
acab
output
OK
OK
abacaba1
OK
input
6
first
first
second
second
third
third
output
OK
first1
OK
second1
OK
third1

题目大意:

邮箱注册用户名是一串字符串,均为小写字母且长度小于30。有N次申请(N<=10^5),如果申请的用户名之前都不存在,输出OK;如果申请的用户名之前存在,就在用户名后面加上后缀数字,如之前存在用户名abc,下一次又申请了abc,那么用户名会修改成abc1,如果下一次又申请了abc,就要修改成abc2,依此类推。

题目分析:

很显然,这是一道Hash的模板题,同时也可以用map来实现。显然用map更容易实现,下面给出map的参考代码。

参考代码:

#include <bits/stdc++.h>
using namespace std;
map<string, int> m;
int main(){
	int n;
	string s;
	cin >> n;
	for(int i=1; i<=n; i++){
		cin >> s;
		m[s] ++;
		if(m[s]==1)
			cout << "OK\n";
		else
			cout << s << m[s]-1 << endl;
	}
	return 0;
}
posted @ 2020-08-02 00:14  gdgzliu  阅读(91)  评论(0编辑  收藏  举报