codeforces 4C. Registration system (字符串hash)

题目链接

题意:网站的注册系统..处理用户要注册的用户名,如果数据库中没有重名输出OK,否则输出要注册的用户名的字符串+num,num的大小为之前一共有多少个用户试图用该用户名。

思路:hash一下。。。

 1/* ***********************************************
 2Author :111qqz
 3Created Time :2016年11月22日 星期二 19时00分58秒
 4File Name :code/cf/problem/4C.cpp
 5************************************************ */
 6
 7#include <cstdio>
 8#include <cstring>
 9#include <iostream>
10#include <algorithm>
11#include <vector>
12#include <queue>
13#include <set>
14#include <map>
15#include <string>
16#include <cmath>
17#include <cstdlib>
18#include <ctime>
19#define fst first
20#define sec second
21#define lson l,m,rt<<1
22#define rson m+1,r,rt<<1|1
23#define ms(a,x) memset(a,x,sizeof(a))
24typedef long long LL;
25#define pi pair < int ,int >
26#define MP make_pair
27
28using namespace std;
29const double eps = 1E-8;
30const int dx4[4]={1,0,0,-1};
31const int dy4[4]={0,-1,1,0};
32const int inf = 0x3f3f3f3f;
33unsigned int  BKDHash(char *str)
34{
35    unsigned int seed = 113;
36    unsigned hash = 0 ;
37    while (*str) hash = hash*seed+(*str++);
38    return (hash&0x7fffffff);
39}
40map<int,int>mp;
41int n;
42const int N=1E5+7;
43char str[33];
44int main()
45{
46	#ifndef  ONLINE_JUDGE 
47	freopen("code/in.txt","r",stdin);
48  #endif
49	cin>>n;
50	for ( int i = 1 ; i <= n ; i++)
51    	{
52	    scanf("%s",str);
53	    int id = BKDHash(str);
54	    if (!mp[id])
55	    {
56		puts("OK");
57		mp[id] = 1;
58	    }
59	    else
60	    {
61		printf("%s%d\n",str,mp[id]);
62		mp[id]++;
63	    }
64
65	}
66
67  #ifndef ONLINE_JUDGE  
68  fclose(stdin);
69  #endif
70    return 0;
71}