题目要求
You are supposed to implement the functions of account"Log in" and "Register" for the most popular instantmessager QQ. The most challenging partis that QQ now has more than a billion users.
Input Specification:
Each input file contains one test case. For each case, the first line contains aninteger N (£105) - the total number ofqueries. Then N lines follow, each contains a query in the format:
Command QQ_No Password
where "Command" is either"R" (meaning to register a new account, and hence followed by a newaccount number and a password), or "L" (meaning to log in with anexisting account, and hence followed by the account number and the password);"QQ_No" is an integer that is greater than 1000 and no more than 10digits long; and "Password" is a string with no less than 6 and nomore than 16 characters without any space.
Output Specification:
For each test case, print the correspondingmessage for each query in a line. Themessages are:
l If a new account is successfullyregistered, output "Register Successful";
l If the new registering account numberalready exists, output "ERROR: Account Number Already Exists";
l If log in successfully, output "Log inSuccessful";
l If the log in account does not exist, output"ERROR: Account Not Exist";
l If log in with a wrong password, output"ERROR: Wrong Password".
Sample Input: 5 L 1234567890 myQQ@qq.com R 1234567890 myQQ@qq.com R 1234567890 myQQ@qq.com L 1234567890 myQQ@qq L 1234567890 myQQ@qq.com
|
Sample Output:
ERROR: Account Not Exist Register Successful ERROR: Account Number Already Exists ERROR: Wrong Password Log in Successful
|
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#define MinTableSize 5
typedef struct QQ* Database;
typedef struct HashEntry Cell;
typedef struct HashT *HashTable;
struct QQ{
char id[15];
char password[20];
};
enum KindOfEntry{Legitimate,Empty,Deleted};
struct HashEntry{
struct QQ user;
enum KindOfEntry Info;
};
struct HashT{
int tablesize;
Cell *thecells;
};
int Isprime(int n){
int i;
if(n == 1)
return 0;
for(i = 2;i*i<=n;i++)
if(n %i == 0)
break;
if(i*i<=n)
return 0;
else return 1;
}
int nextprime(int n){
while(!Isprime(n)){
n++;
}
return n;
}
int Hash(const char *key,int n){
int hashval;
hashval = 0;
while(*key != '\0'){
hashval = (hashval << 1) + *key++;
}
return hashval % n;
}
HashTable Initialize(int tablesize){
HashTable h;
int i;
if(tablesize<MinTableSize){
perror("Table size too small\n");
return NULL;
}
h = (HashTable)malloc(sizeof(struct HashT));
if(h == NULL){
perror("Out of Space\n");
return NULL;
}
h->tablesize = nextprime(tablesize);
h->thecells = (Cell*)malloc(sizeof(Cell)*h->tablesize);
if(h->thecells == NULL){
perror("Out of Space\n");
return NULL;
}
for(i = 0;i<h->tablesize;i++)
h->thecells[i].Info = Empty;
return h;
}
int Find(char *key,HashTable h){
int currentpos;
int collisionnum;
collisionnum = 0;
currentpos = Hash(key,h->tablesize);
while(h->thecells[currentpos].Info != Empty&&strcmp(h->thecells[currentpos].user.id,key)){
currentpos += 2*++collisionnum-1;
if(currentpos>=h->tablesize)
currentpos -= h->tablesize;
}
return currentpos;
}
void Insert(struct QQ key,HashTable h){
int pos;
pos = Find(key.id,h);
if(h->thecells[pos].Info!=Legitimate){
h->thecells[pos].Info = Legitimate;
h->thecells[pos].user = key;
}
}
struct QQ *findcount(HashTable h,char *count){
int pos;
pos = Find(count,h);
if(h->thecells[pos].Info == Empty)
return NULL;
else return &h->thecells[pos].user;
}
void CountInsert(HashTable h,char *count,char *pwd){
struct QQ user;
strcpy(user.id,count);
strcpy(user.password,pwd);
Insert(user,h);
}
HashTable ReHash(HashTable h){
Cell *oldcells;
int oldsize,i;
oldcells = h->thecells;
oldsize = h->tablesize;
h = Initialize(oldsize*2);
for(i = 0;i<oldsize;i++)
if(oldcells[i].Info == Legitimate)
Insert(oldcells[i].user,h);
free(oldcells);
return h;
}
int main(){
Database r,d,p;
int n,size;
HashTable h;
char cmd,count[15],pwd[20],*s;
scanf("%d",&n);
getchar();
r = NULL;
size = 0;
h = Initialize(100000);
while(n--){
scanf("%c%s%s",&cmd,&count,&pwd);
getchar();
d = findcount(h,count);
if(cmd == 'L'){
if(d == NULL)
printf("ERROR: Account Not Exist\n");
else if(strcmp(d->password,pwd))
printf("ERROR: Wrong Password\n");
else printf("Log in Successful\n");
}
else if(cmd == 'R') {
if(d != NULL)
printf("ERROR: Account Number Already Exists\n");
else{
printf("Register Successful\n");
CountInsert(h,count,pwd);
size++;
//if(size == h->tablesize/2)
//h = ReHash(h);
}
}
}
return 0;
}
AVL TREE
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct QQ* Database;
struct QQ{
char id[15];
char password[20];
int height;
Database left,right;
};
//Tree search
Database findcount(Database d,char *id){
while(d != NULL){
if(strcmp(id,d->id) == 0) break;
else if(strcmp(id,d->id)<0) d = d->left;
else if(strcmp(id,d->id)>0) d = d->right;
}
return d;
}
//AVL tree
int height(Database r){
if(r == NULL)
return -1;
else return r->height;
}
Database SingleRotateWithLeft(Database p){
Database q;
q = p->left;
p->left = q->right;
q->right = p;
p->height = height(p->left)>height(p->right)?height(p->left) + 1:height(p->right) + 1;
q->height = height(q->left)>height(q->right)?height(q->left) + 1:height(q->right) + 1;
return q;
}
Database SingleRotateWithRight(Database p){
Database q;
q = p->right;
p->right = q->left;
q->left = p;
p->height = height(p->left)>height(p->right)?height(p->left) + 1:height(p->right) + 1;
q->height = height(q->left)>height(q->right)?height(q->left) + 1:height(q->right) + 1;
return q;
}
Database DoubleRotateWithLeft(Database p){
p->left = SingleRotateWithRight(p->left);
return SingleRotateWithLeft(p);
}
Database DoubleRotateWithRight(Database p){
p->right = SingleRotateWithLeft(p->right);
return SingleRotateWithRight(p);
}
Database treeinsert(Database r,char *id,char *pwd){
Database p;
if(r == NULL){
r = (Database)malloc(sizeof(struct QQ));
if(r == NULL){
printf("Out of Space\n");
return NULL;
}
strcpy(r->id,id);
strcpy(r->password,pwd);
r->left = r->right = NULL;
r->height = 0;
}
else if(strcmp(id,r->id)<0){
r->left = treeinsert(r->left,id,pwd);
if(height(r->left) - height(r->right) == 2)
if(strcmp(id,r->left->id)<0)
r = SingleRotateWithLeft(r);
else r = DoubleRotateWithLeft(r);
}
else if(strcmp(id,r->id)>0){
r->right = treeinsert(r->right,id,pwd);
if(height(r->right) - height(r->left) == 2)
if(strcmp(id,r->right->id)>0)
r = SingleRotateWithRight(r);
else r = DoubleRotateWithRight(r);
}
r->height = height(r->left)>height(r->right)?height(r->left)+1:height(r->right)+1;
return r;
}
int main(){
Database r,d,p;
int n;
char cmd,count[15],pwd[20],*s;
scanf("%d",&n);
getchar();
r = NULL;
while(n--){
scanf("%c%s%s",&cmd,&count,&pwd);
getchar();
d = findcount(r,count);
if(cmd == 'L'){
if(d == NULL)
printf("ERROR: Account Not Exist\n");
else if(strcmp(d->password,pwd))
printf("ERROR: Wrong Password\n");
else printf("Log in Successful\n");
}
else if(cmd == 'R') {
if(d != NULL)
printf("ERROR: Account Number Already Exists\n");
else{
printf("Register Successful\n");
r = treeinsert(r,count,pwd);
}
}
}
return 0;
}