C. Registration system

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

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

Description

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.

Sample Input

4
abacaba
acaba
abacaba
acab

Sample Output

OK
OK
abacaba1
OK

HINT

题意

如果这个词第一次出现,输出ok

否则输出这个词,并且输出在此之前这个词出现了多少次

题解:

双hash+map就好了

代码

  1. //qscqesze
  2. #include <cstdio>
  3. #include <cmath>
  4. #include <cstring>
  5. #include <ctime>
  6. #include <iostream>
  7. #include <algorithm>
  8. #include <set>
  9. #include <vector>
  10. #include <sstream>
  11. #include <queue>
  12. #include <typeinfo>
  13. #include <fstream>
  14. #include <map>
  15. #include <stack>
  16. typedef long long ll;
  17. using namespace std;
  18. //freopen("D.in","r",stdin);
  19. //freopen("D.out","w",stdout);
  20. #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
  21. #define maxn 200001
  22. #define mod 10007
  23. #define eps 1e-9
  24. int Num;
  25. char CH[];
  26. //const int inf=0x7fffffff; //нчоч╢С
  27. const int inf=0x3f3f3f3f;
  28. /*
  29.  
  30. inline void P(int x)
  31. {
  32. Num=0;if(!x){putchar('0');puts("");return;}
  33. while(x>0)CH[++Num]=x%10,x/=10;
  34. while(Num)putchar(CH[Num--]+48);
  35. puts("");
  36. }
  37. */
  38. inline ll read()
  39. {
  40. int x=,f=;char ch=getchar();
  41. while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
  42. while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
  43. return x*f;
  44. }
  45. inline void P(int x)
  46. {
  47. Num=;if(!x){putchar('');puts("");return;}
  48. while(x>)CH[++Num]=x%,x/=;
  49. while(Num)putchar(CH[Num--]+);
  50. puts("");
  51. }
  52. //**************************************************************************************
  53.  
  54. int get_hash(char *key)
  55. {
  56. ll N=;
  57. long long h=;
  58. while(*key)
  59. h=(h*+(*key++)+N)%N;
  60. return h%N;
  61. }
  62. int get_hash2(char *key)
  63. {
  64. ll N=;
  65. long long h=;
  66. while(*key)
  67. h=(h*+(*key++)+N)%N;
  68. return h%N;
  69. }
  70. char s[];
  71. map< pair<int,int> ,int>H;
  72. int main()
  73. {
  74. int n=read();
  75. for(int i=;i<n;i++)
  76. {
  77. scanf("%s",s);
  78. pair<int,int> a;
  79. a.first=get_hash(s);
  80. a.second=get_hash2(s);
  81. H[a]++;
  82. if(H[a]==)
  83. printf("OK\n");
  84. else printf("%s%d\n",s,H[a]-);
  85. }
  86. }

Codeforces Beta Round #4 (Div. 2 Only) C. Registration system hash的更多相关文章

  1. Codeforces Beta Round #4 (Div. 2 Only) C. Registration system【裸hash/map】

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

  2. Codeforces Beta Round #80 (Div. 2 Only)【ABCD】

    Codeforces Beta Round #80 (Div. 2 Only) A Blackjack1 题意 一共52张扑克,A代表1或者11,2-10表示自己的数字,其他都表示10 现在你已经有一 ...

  3. Codeforces Beta Round #83 (Div. 1 Only)题解【ABCD】

    Codeforces Beta Round #83 (Div. 1 Only) A. Dorm Water Supply 题意 给你一个n点m边的图,保证每个点的入度和出度最多为1 如果这个点入度为0 ...

  4. Codeforces Beta Round #79 (Div. 2 Only)

    Codeforces Beta Round #79 (Div. 2 Only) http://codeforces.com/contest/102 A #include<bits/stdc++. ...

  5. Codeforces Beta Round #77 (Div. 2 Only)

    Codeforces Beta Round #77 (Div. 2 Only) http://codeforces.com/contest/96 A #include<bits/stdc++.h ...

  6. Codeforces Beta Round #76 (Div. 2 Only)

    Codeforces Beta Round #76 (Div. 2 Only) http://codeforces.com/contest/94 A #include<bits/stdc++.h ...

  7. Codeforces Beta Round #75 (Div. 2 Only)

    Codeforces Beta Round #75 (Div. 2 Only) http://codeforces.com/contest/92 A #include<iostream> ...

  8. Codeforces Beta Round #74 (Div. 2 Only)

    Codeforces Beta Round #74 (Div. 2 Only) http://codeforces.com/contest/90 A #include<iostream> ...

  9. Codeforces Beta Round #73 (Div. 2 Only)

    Codeforces Beta Round #73 (Div. 2 Only) http://codeforces.com/contest/88 A 模拟 #include<bits/stdc+ ...

随机推荐

  1. 13、NFC技术:读写非NDEF格式的数据

    MifareUltralight数据格式 将NFC标签的存储区域分为16个页,每一个页可以存储4个字节,一个可存储64个字节(512位).页码从0开始(0至15).前4页(0至3)存储了NFC标签相关 ...

  2. Delphi 串口通信数据位长度对传输数据的影响 转

      针对串口通信,关于设置数据位长度对通信的影响,如图: 在串口数据通信中,会看到串口参数设置.其中“数据位”设置,共有四档选项,分别是8.7.6.5.那么改变这个参数会对数据的传输有什么影响呢? 我 ...

  3. php常用正则

    平时做网站经常要用正则表达式,下面是一些讲解和例子,仅供大家参考和修改使用: 2.    "^\d+$" //非负整数(正整数 + 0) 3.    "^[0-9]*[1 ...

  4. stitch image app (Qt)

    result how to use? source code http://7qnct6.com1.z0.glb.clouddn.com/dropSelect.rar

  5. python开源包提交到pypi社区

    为啥要提交到pypi?因为提交成功后,你今后想用你自己写的模块,只要pip install一下就可以了. 那么如何提交?请参看本篇教程 首先要确定你的包叫啥名,比如我的包叫xlutils3,既然确定了 ...

  6. linux 常用端口列表

    常见端口表汇总 1 tcpmux TCP Port Service Multiplexer 传输控制协议端口服务多路开关选择器 2 compressnet Management Utility com ...

  7. Javascript模块化开发-轻巧自制

    Javascript模块化开发-轻巧自制 一.前言现在javascript的流行,前端的代码越来越复杂,所以我们需要软件工程的思想来开发前端.模块化是必不可少的,这样不仅能够提高代码的可维护性.可扩展 ...

  8. 序列for循环语句

    序列for循环语句 序列for循环语句允许重复遍历一组序列,而这组序列可以是任何可以重复遍历的序列,如由begin()和end()函数定义的STL序列.所有的标准容器都可用作这种序列,同时它也同样可以 ...

  9. Intent传递数据

    方式比较多,先看看代码,一会儿再总结. activity_main.xml <RelativeLayout xmlns:android="http://schemas.android. ...

  10. 使用RSA非对称密钥算法实现硬件设备授权

    一.硬件设备授权 即用户在硬件设备输入一个序列号(或一个包含授权信息的文件),然后硬件设备便可正常使用.    二.授权方案 构思授权方案时,参考了下面网址的思路: http://bbs.csdn.n ...