Codeforces Beta Round #4 (Div. 2 Only) C. Registration system 暴力or 字典树or哈希
http://codeforces.com/problemset/problem/4/C
题意:
一个注册系统,给出n个注册的名字,每次检查数据库中是否存在该名字,如果不存在返回OK,加入数据库,否如果存在在该名字后边加1,2,3,,,....然后加入数据库。
思路:
暴力存储查询可过,用字典数存处名字可过,哈希存储查找可过。
暴力:
View Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string> #define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a,b) (a) > (b)? (b):(a) #define Max(a,b) (a) > (b)? (a):(b) #define ll long long #define inf 0x7f7f7f7f #define MOD 1073741824 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 1000007 #define N 100007 using namespace std; //freopen("data.in","r",stdin); string str[N]; int num[N]; int len; bool isok(string a,string &b){ for (int i = 0; i < len; ++i){ if (str[i] == a){ char p[10]; sprintf(p,"%d",num[i]); b = a + p; num[i]++; return false; } } str[len] = a; num[len++]++; return true; } int main(){ //freopen("data.in","r",stdin); int i; int n; cin>>n; string tmp,stre; CL(num,0); len = 0; for (i = 0; i < n; ++i){ cin>>stre; if (isok(stre,tmp)) puts("OK"); else{ cout<<tmp<<endl; } } return 0; }
字典数:
View Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string> #define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a,b) (a) > (b)? (b):(a) #define Max(a,b) (a) > (b)? (a):(b) #define ll long long #define inf 0x7f7f7f7f #define MOD 1073741824 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 1000007 #define N 100007 using namespace std; //freopen("data.in","r",stdin); struct node{ int flag; node *next[27]; }*head,H[500000]; int t; char str[40],tmp[10]; node* newnode(){ node *p = &H[t++]; for (int i = 0; i < 26; ++i){ p->next[i] = NULL; p->flag = 0; } return p; } bool find(char *s,char *tmp){ int i,k; int len = strlen(s); node *p = head; for (i = 0; i < len; ++i){ k = s[i] - 'a'; if (p->next[k] == NULL) return false; p = p->next[k]; } if (p->flag != 0){ sprintf(tmp,"%d",p->flag); p->flag++; return true; } else return false; } void insert(char *s){ int i,k; int len = strlen(s); node *p = head; for (i = 0; i < len; ++i){ k = s[i] - 'a'; if (p->next[k] == NULL) p->next[k] = newnode(); p = p->next[k]; } p->flag++; } int main(){ //freopen("data.in","r",stdin); int i; int n; cin>>n; t = 0; head = newnode(); for (i = 0; i < n; ++i){ scanf("%s",str); if (find(str,tmp)) printf("%s%s\n",str,tmp); else{ puts("OK"); insert(str); } } return 0; }
哈希:
View Code
#include <iostream> #include <cstdio> #include <cstdlib> #include <cstring> #include <algorithm> #include <cmath> #include <queue> #include <stack> #include <set> #include <map> #include <string> #define CL(a,num) memset((a),(num),sizeof(a)) #define iabs(x) ((x) > 0 ? (x) : -(x)) #define Min(a,b) (a) > (b)? (b):(a) #define Max(a,b) (a) > (b)? (a):(b) #define ll long long #define inf 0x7f7f7f7f #define MOD 1073741824 #define lc l,m,rt<<1 #define rc m + 1,r,rt<<1|1 #define pi acos(-1.0) #define test puts("<------------------->") #define maxn 100007 #define M 1000007 #define N 100007 using namespace std; //freopen("data.in","r",stdin); struct node{ string str; int num; node *next; }*head[N + 2],H[N]; int t; char str[40],tmp[10]; bool Cha(int sum,char *s,char*tmp){ sum %= N; if (sum < 0) sum = -sum; node *p; for (p = head[sum]; p != NULL; p = p->next){ if (p->str == s){ sprintf(tmp,"%d",p->num); p->num++; return true; } } return false; } void Gua(int sum,char *s){ sum %= N; if (sum < 0) sum = -sum; node *p = &H[t++]; p->str = s; p->num = 1; p->next = head[sum]; head[sum] = p; } int main(){ //freopen("data.in","r",stdin); int n,i,j; CL(head,0); t = 0; scanf("%d",&n); int sum = 0; for (i = 0; i < n; ++i){ scanf("%s",str); int len = strlen(str); sum = 0; for (j = 0; j < len; ++j){ sum += str[j] - '0'; } if (Cha(sum,str,tmp)) printf("%s%s\n",str,tmp); else{ puts("OK"); Gua(sum,str); } } return 0; }