Codeforces Beta Round #4 (Div. 2 only) C. Registration system Hash

Source: Internet
Author: User

C. Registration system

Time Limit:20 Sec

Memory limit:256 MB

Topic Connection

Http://codeforces.com/problemset/problem/4/C

Descriptiona new e-mail Service "Berlandesk" is going to being opened in Berland The site administration wants to launch their project as soon as possible, that's why they ask for 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's 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 a ND also inserts the prompt into the database. The new name is formed by the following rule. Numbers, starting with 1, was appended one after another to name (Name1, name2, ...), among these Numbers the least I is F Ound So, 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 was a non-empty line, and consists of not more than characters, which was all lowercase Latin.

Output

Print n lines, which is system responses to the Requests:ok in case of successful registration, or a prompt with a new n AME, if the requested name is already taken.

Sample Input

4
Abacaba
Acaba
Abacaba
Acab

Sample Output

Ok
Ok
Abacaba1
Ok

HINT

Test instructions

If the word appears for the first time, output OK

Otherwise the word is output, and how many times does the word appear before the output

Exercises

Double Hash+map just fine.

Code

//Qscqesze#include <cstdio>#include<cmath>#include<cstring>#include<ctime>#include<iostream>#include<algorithm>#include<Set>#include<vector>#include<sstream>#include<queue>#include<typeinfo>#include<fstream>#include<map>#include<stack>typedefLong Longll;using namespacestd;//freopen ("d.in", "R", stdin);//freopen ("D.out", "w", stdout);#defineSspeed ios_base::sync_with_stdio (0); Cin.tie (0)#defineMAXN 200001#defineMoD 10007#defineEPS 1e-9intNum;Charch[ -];//const int INF=0X7FFFFFFF; //нчоч╢сConst intinf=0x3f3f3f3f;/*inline void P (int x) {num=0;if (!x) {Putchar (' 0 ');p UTS (""); return;}    while (x>0) ch[++num]=x%10,x/=10;    while (Num) Putchar (ch[num--]+48); Puts ("");}*/inline ll read () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}  while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();} returnx*F;} InlinevoidPintx) {Num=0;if(!x) {Putchar ('0');p UTS ("");return;}  while(x>0) ch[++num]=x%Ten, x/=Ten;  while(Num) Putchar (ch[num--]+ -); Puts ("");}//**************************************************************************************intGet_hash (Char*key) {ll N=1998585857; Long LongH=0;  while(*key) H= (h*127+ (*key++) +n)%N; returnh%N;}intGET_HASH2 (Char*key) {ll N=127398127; Long LongH=0;  while(*key) H= (h*127+ (*key++) +n)%N; returnh%N;}Chars[ +];map< pair<int,int>,int>H;intMain () {intn=read ();  for(intI=0; i<n;i++) {scanf ("%s", s); Pair<int,int>A; A.first=Get_hash (s); A.second=Get_hash2 (s); H[a]++; if(h[a]==1) printf ("ok\n"); Elseprintf"%s%d\n", s,h[a]-1); }}

Codeforces Beta Round #4 (Div. 2 only) C. Registration system Hash

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.