11-hash 3 QQ account application and login (25 points)

Posted by Option on Wed, 25 Mar 2020 01:56:19 +0100

Simplified version of QQ new account application and old account login.The biggest challenge is: it is said that the QQ number now has 10 digits.

Input format:

Input first gives a positive integer NN (< 10^5), followed by a NN line instruction.The format of each line of instructions is "Command (space) QQ number (space) password".When the command character is "N" (for New), it means to apply for a new QQ number followed by the number and password of the new account; when the command character is "L" (for Login), it means that the old account is logged on, followed by the logon information.The QQ number is an integer with no more than 10 digits but greater than 1000 (it is said that the total number of QQ is 001).A string with a password of no less than 6 bits, no more than 16 bits, and no spaces.

Output format:

For each instruction, give the appropriate information:

1) If the new application account is successful, output "New: OK";
2) If the number of the new application already exists, output "ERROR: Exist";
3) If the old account logs on successfully, output "Login: OK";
4) If the QQ number of the old account does not exist, output "ERROR: Not Exist";
5) If the old account password is wrong, output "ERROR: Wrong PW".



Input sample:

5
L 1234567890 myQQ@qq.com
N 1234567890 myQQ@qq.com
N 1234567890 myQQ@qq.com
L 1234567890 myQQ@qq
L 1234567890 myQQ@qq.com

Output sample:

ERROR: Not Exist
New: OK
ERROR: Exist
ERROR: Wrong PW
Login: OK

Solution 1: A Hash list stores qq accounts and passwords (with Telephone chat maniac Similar solution)

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define KEYLENGTH 10
#define MAXTABLESIZE 100000
typedef char ElementType[KEYLENGTH + 1];
typedef char PasswordType[17];
typedef int Index;

typedef struct LNode *PtrToLNode;
struct LNode {
	ElementType Data;
	PtrToLNode Next;
	PasswordType Pw;	//Add password
};
typedef struct LNode *List;
typedef struct LNode *Position;

typedef struct TblNode *HashTable;
struct TblNode {
	int TableSize;
	List Heads;
};

int NextPrime ( int n ) {
	if ( n == 1 ) 
		return 2;
	else {
		int i, p = n % 2 ? n + 2 : n + 1;
		while ( p <= MAXTABLESIZE ) {
			double q = p;
			for ( i = sqrt(q); i > 2; i-- )
				if ( p % i ) break;
			if ( i == 2 ) break;
			else p += 2;
		}
		return p;
	}
}

HashTable CreateTable ( int TableSize ) {
	HashTable H;
	H = (HashTable)malloc(sizeof(struct TblNode));
	H->TableSize = NextPrime( TableSize );
	H->Heads = (List)malloc(H->TableSize * sizeof(struct LNode));
	for ( int i = 0; i < H->TableSize; i++ ) {
		H->Heads[i].Data[0] = '\0';
		H->Heads[i].Pw[0] = '\0';  //Initialization
		H->Heads[i].Next = NULL;
	}
	return H;
}

Index Hash ( const char *key, int TableSize ) {
	unsigned int h = 0;
	while ( *key != '\0' ) {
		h = ( h << 5 ) + *key++;
	}
	return h % TableSize;
}

Position Find ( HashTable H, ElementType Key ) {
	Position P;
	Index pos;
	pos = Hash( Key + 3, H->TableSize );
	P = H->Heads[pos].Next;
	while ( P && strcmp( P->Data, Key ) ) 
		P = P->Next;
	return P;
}

bool Insert ( HashTable H, ElementType Key, PasswordType Pw) {
	Index pos;
	if ( Find( H, Key ) ) 
		return false;
	else {
		Position NewCell = (Position)malloc(sizeof(struct LNode));
		strcpy( NewCell->Data, Key );
		strcpy( NewCell->Pw, Pw );  //Record password
		pos = Hash( Key + 3, H->TableSize );
		NewCell->Next = H->Heads[pos].Next;
		H->Heads[pos].Next = NewCell;
		return true;
	}
}

void DestroyTable ( HashTable H ) {
	int i;
	Position Tmp, P;
	for ( i = 0; i < H->TableSize; i++ ) {
		P = H->Heads[i].Next;
		while ( P != NULL ) {
			Tmp = P;
			P = P->Next;
			free(Tmp);
		}
	}
	free( H->Heads );
	free( H );
}

//Check if the password is correct
bool CheckPassword ( Position P, char pw[] ) {
	if ( strcmp( pw, P->Pw ) )
		return false;
	else
		return true;
}

//Check Account Existence
bool FindAccount ( HashTable H, char qq[] ) {
	Position P;
	Index pos;
	pos = Hash( qq + 3, H->TableSize );
	P = H->Heads[pos].Next;
	while ( P != NULL ) {
		if ( strcmp( P->Data, qq ) == 0 )
			return 1;
		P = P->Next;
	}
	return 0;
}

int main () {
	int N;
	char c, qq[11], pw[17];
	HashTable H;
	scanf("%d", &N);
	getchar();
	H = CreateTable( 2 * N );
	while ( N-- ) {
		scanf("%c %s %s", &c, qq, pw);
		getchar();
		if ( c == 'L' ) {
			if ( FindAccount( H, qq ) ) {
				if ( CheckPassword( Find( H, qq ), pw ) )
					printf("Login: OK\n");
				else 
					printf("ERROR: Wrong PW\n");
			}
			else 
				printf("ERROR: Not Exist\n");
		}
		else {
			if ( FindAccount( H, qq) ) 
				printf("ERROR: Exist\n");
			else {
				printf("New: OK\n");
				Insert( H, qq, pw );
			}
		}
	}
	DestroyTable( H );
	system("pause");
	return 0;
}

Solution 2: Sort, using map containers

#include <cstdio>
#include <string>
#include <map>
#include <cstdlib>
using namespace std;
int main () {
	int N;
	char c, s1[11], s2[17];
	string qq, pw;
	map<string, string> m;
	scanf("%d", &N);
	getchar();
	while ( N-- ) {
		scanf("%c %s %s", &c, s1, s2);
		getchar();
		qq = s1; pw = s2;
		if ( c == 'N') {
			if ( m.find(qq) != m.end() )
				printf("ERROR: Exist\n");
			else {
				printf("New: OK\n");
				m[qq] = pw;
			}
		}
		else {
			if ( m.find(qq) == m.end() )
				printf("ERROR: Not Exist\n");
			else if ( m[qq] == pw )
				printf("Login: OK\n");
			else 
				printf("ERROR: Wrong PW\n");
		}
	}
	system("pause");
	return 0;
}







63 original articles have been published. 38. Access 110,000+
Private letter follow

Topics: less