1. 程式人生 > >nyoj Registration system

nyoj Registration system

ould service main type ace RM -m 區別 ken

Registration system

時間限制:1000 ms | 內存限制:65535 KB 難度:2
描述

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.

技術分享圖片

輸入
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 1000 characters, which are all lowercase Latin letters.
輸出
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.
樣例輸入
4
abacaba
acaba
abacaba
acab
樣例輸出
OK
OK
abacaba1
OK

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

int main()
{
int n;
map<string,int> m;
cin>>n;
while(n--)
{
string s;
cin>>s;

if(m[s])
cout<<s<<m[s]<<endl;
else
cout<<"OK"<<endl;
m[s]++;
}
return 0;
}

map:數據的插入

在構造map容器後,我們就可以往裏面插入數據了。這裏講三種插入數據的方法:
第一種:用insert函數插入pair數據
map<int, string> mapStudent;
mapStudent.insert(pair<int, string>(1,“student_one”));
第二種:用insert函數插入value_type數據
map<int, string> mapStudent;
mapStudent.insert(map<int, string>::value_type (1,"student_one")); mapStudent.insert(make_pair(1, "student_one"));
第三種:用數組方式插入數據 map<int, string> mapStudent;
mapStudent[1] = “student_one”;
mapStudent[2] = “student_two”; /* 如果是
#include <map>
map<string, int> mapStudent; string s;
插入就用m[s]++; */ 以上三種用法,雖然都可以實現數據的插入,但是它們是有區別的,當然了第一種和第二種在效果上是完成一樣的,用insert函數插入數據,在數據的插入上涉及到集合的唯一性這個概念,即當map中有這個關鍵字時,insert操作是不能再插入這個數據的,但是用數組方式就不同了,它可以覆蓋以前該關鍵字對應的值,即:如果當前存在該關鍵字,則覆蓋改關鍵字的值,否則,以改關鍵字新建一個key—value;

nyoj Registration system