codeforces 4C. Registration system (字符串hash)
题意:网站的注册系统..处理用户要注册的用户名,如果数据库中没有重名输出OK,否则输出要注册的用户名的字符串+num,num的大小为之前一共有多少个用户试图用该用户名。
思路:hash一下。。。
/* ***********************************************
Author :111qqz
Created Time :2016年11月22日 星期二 19时00分58秒
File Name :code/cf/problem/4C.cpp
************************************************ */
1#include <cstdio>
2#include <cstring>
3#include <iostream>
4#include <algorithm>
5#include <vector>
6#include <queue>
7#include <set>
8#include <map>
9#include <string>
10#include <cmath>
11#include <cstdlib>
12#include <ctime>
13#define fst first
14#define sec second
15#define lson l,m,rt<<1
16#define rson m+1,r,rt<<1|1
17#define ms(a,x) memset(a,x,sizeof(a))
18typedef long long LL;
19#define pi pair < int ,int >
20#define MP make_pair
1using namespace std;
2const double eps = 1E-8;
3const int dx4[4]={1,0,0,-1};
4const int dy4[4]={0,-1,1,0};
5const int inf = 0x3f3f3f3f;
6unsigned int BKDHash(char *str)
7{
8 unsigned int seed = 113;
9 unsigned hash = 0 ;
10 while (*str) hash = hash*seed+(*str++);
11 return (hash&0x7fffffff);
12}
13map<int,int>mp;
14int n;
15const int N=1E5+7;
16char str[33];
17int main()
18{
19 #ifndef ONLINE_JUDGE
20 freopen("code/in.txt","r",stdin);
21 #endif
22 cin>>n;
23 for ( int i = 1 ; i <= n ; i++)
24 {
25 scanf("%s",str);
26 int id = BKDHash(str);
27 if (!mp[id])
28 {
29 puts("OK");
30 mp[id] = 1;
31 }
32 else
33 {
34 printf("%s%d\n",str,mp[id]);
35 mp[id]++;
36 }
}
1 #ifndef ONLINE_JUDGE
2 fclose(stdin);
3 #endif
4 return 0;
5}