MOOC data Structure 5-15 QQ account application and landing (25 points) _----Hash & Hash

Source: Internet
Author: User
Tags strcmp


To achieve the QQ new account application and the old account landing simplified version of the function. The biggest challenge is: it is said that the current QQ number has 10 digits. Input format:

The input first gives a positive integer nn (\le 10^5≤10 5) and then gives the nn line instruction. The format of each line of instruction is: "Command (space) QQ number (space) password". Where the command character is "N" (represents new), it means to apply for a new QQ number, followed by the number and password of the new account; the command character "L" (representing login) means that the old account is logged in, followed by the login information. QQ number is not more than 10 digits, but greater than 1000 (it is said that QQ Mister Number is 1001) of the Integer. The password is a string that is not less than 6 bits, does not exceed 16 bits, and does not contain spaces. Output format:

For each instruction, give the appropriate information:

1 if the new application account succeeds, the output "New:ok";
2 If the number of the new application already exists, then the output "error:exist";
3 If the old account landing successful, then output "Login:ok";
4 If the old account QQ number does not exist, then output "Error:not Exist";
5 If the old account password is wrong, then 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


Thought analysis: Examination hashing. Chain separation method, because the title said QQ number is a number greater than 1000, so here take the first 4 digits of the QQ string as the initial hash address, the time is about 800MS. (after testing, the top 5 can also be AC, and time efficiency is greatly improved, about 150MS)


#include <cstdio> #include <cstdlib> #include <string.h> typedef struct Node {char user[11];
    Char password[17];
struct node* next;

Node;
    typedef struct {int tablesize;
node* Table;

} HashTable;
        int nextprime (int n) {while (1) {bool flag = true;
                for (int i = 2; i < n; i++) {if (n% i = = 0) {flag = false;
            Break
        } if (flag) break;
    n++;
} return N;
    } hashtable* createhashtable (int n) {hashtable* H = (hashtable*) malloc (sizeof (HashTable));

    H->tablesize = NextPrime (n);

    H->table = (node*) malloc ((h->tablesize) * sizeof (Node));
        for (int i = 0; i < h->tablesize; i++) {h->table[i].next = NULL;
        H->table[i].user[0] = ' the ';
    H->table[i].password[0] = ' the ';
return H;
    int Hash (char key[]) {int index = 0; for (int i = 0; I &lT;= 3;
    i++) {index = index * + key[i]-' 0 ';
} return index;

    } node* Find (hashtable* H, char key[]) {int index = Hash (key)% (h->tablesize);

    node* ptr = h->table[index].next;
    while (PTR && strcmp (Ptr->user, key)) {ptr = ptr->next;
} return ptr;

    } void Insert (hashtable* H, Char key[], char pass[]) {int index = Hash (key)% (h->tablesize);

    node* NewNode = (node*) malloc (sizeof (Node));
    Newnode->next = h->table[index].next;

    H->table[index].next = NewNode;
    strcpy (Newnode->user, key);
strcpy (Newnode->password, pass);
    int main () {//freopen ("123.txt", "R", stdin);

    int n;

    scanf ("%d", &n);

    hashtable* H = createhashtable (n);

    printf ("%d\n", h->tablesize);
    Char op;
    Char account[20];

    Char key[20];
        for (int i = 0; i < n; i++) {Char ch = getchar (); scanf ("%c%s%s", &op,account, key);
        printf ("%s%s\n", account, key);
            if (op = = ' L ') {node* p = find (H, account);    if (!p) printf ("Error:not exist\n"); The old account number does not exist else if (strcmp (P->password, key)) {//Old account password error printf ("Error:wrong PW
            \ n ");
            else {//Login succeeded printf ("login:ok\n");
            } if (op = = ' N ') {node* p = find (H, account);
            if (p) {//Application account already exists printf ("error:exist\n");
                else {Insert (H, account, key);
            printf ("new:ok\n");
}} return 0;
 }


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.