11-散列3 QQ帐户的申请与登陆 (25 分)(哈希/map)

9 篇文章 0 订阅
5 篇文章 0 订阅

实现QQ新帐户申请和老帐户登陆的简化版功能。最大挑战是:据说现在的QQ号码已经有10位数了。

输入格式:
输入首先给出一个正整数N(≤10
​5
​​ ),随后给出N行指令。每行指令的格式为:“命令符(空格)QQ号码(空格)密码”。其中命令符为“N”(代表New)时表示要新申请一个QQ号,后面是新帐户的号码和密码;命令符为“L”(代表Login)时表示是老帐户登陆,后面是登陆信息。QQ号码为一个不超过10位、但大于1000(据说QQ老总的号码是1001)的整数。密码为不小于6位、不超过16位、且不包含空格的字符串。

输出格式:
针对每条指令,给出相应的信息:

1)若新申请帐户成功,则输出“New: OK”;
2)若新申请的号码已经存在,则输出“ERROR: Exist”;
3)若老帐户登陆成功,则输出“Login: OK”;
4)若老帐户QQ号码不存在,则输出“ERROR: Not Exist”;
5)若老帐户密码错误,则输出“ERROR: Wrong PW”。

输入样例:
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
输出样例:
ERROR: Not Exist
New: OK
ERROR: Exist
ERROR: Wrong PW
Login: OK

Solution 1:哈希

思路:对于每条指令,如果为L,去找这个账号是否存在。如果存在,再去判断密码是否正确;如果不存在,直接输出相应结果。如果为N,去找这个账号是否存在。如果存在,输出相应结果;如果不存在,将该账号存入表中。

  • 本题用分离链接法解决冲突,并取账号后7位模哈希表大小作为哈希函数。

代码如下:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define maxsize 200000
typedef struct lnode* List;
typedef struct htlnode* hashtable;
struct lnode
{
    char s1[15], s2[20];
    List next;
};
struct htlnode
{
    int tablesize;
    List heads;
};
int isprime(int x)
{
    int i;
    for(i=2; i*i<=x; i++)
        if(x%i == 0) return 0;
    return 1;
}
int nextprime(int n)
{
    if( n == 1 ) return 2;
    int x = n%2 == 1 ? n : n+1;
    while(x <= maxsize)
    {
        if(isprime(x)) return x;
        x += 2;
    }
}
hashtable create(int n)
{
    hashtable h = (hashtable)malloc(sizeof(struct htlnode));
    h->tablesize = nextprime(n);
    h->heads = (List)malloc(h->tablesize*sizeof(struct lnode));
    int i;
    for(i=0; i<h->tablesize; i++)
    {
        h->heads[i].s1[0] = h->heads[i].s2[0] = '\0';
        h->heads[i].next = NULL;
    }
    return h;
}
int Hash(int x, int p)
{
    return x % p;
}
List Find(hashtable h, char s[])
{
    int pos = Hash(atoi(s+3), h->tablesize);
    List p = h->heads[pos].next;
    while(p && strcmp(p->s1, s)!=0) p = p->next;
    return p;
}
void Insert(hashtable h, char account[], char password[])
{
    List p = Find(h, account);
    if(!p)
    {
        List newitem = (List)malloc(sizeof(struct lnode));
        strcpy(newitem->s1, account);
        strcpy(newitem->s2, password);
        int pos = Hash(atoi(account+3), h->tablesize);
        newitem->next = h->heads[pos].next;/* 新元素插入表头 */
        h->heads[pos].next = newitem;
        printf("New: OK\n");
    }
    else printf("ERROR: Exist\n");
}
int main()
{
    int n;
    char c, account[15], password[20];
    scanf("%d", &n);
    hashtable h = create(n);
    while(n--)
    {
        getchar();/* 接受回车 */
        scanf("%c%s%s", &c, account, password);
        if(c == 'N') Insert(h, account, password);
        else if(c == 'L')
        {
            List p = Find(h, account);
            if(!p) printf("ERROR: Not Exist\n");
            else
            {
                if(strcmp(p->s2, password) == 0) printf("Login: OK\n");
                else printf("ERROR: Wrong PW\n");
            }
        }
    }
    return 0;
}


Solution 2:map容器

os:刚开始学哈希,所以还是老老实实敲出了数据结构的代码。在做题的过程中碰到了一些麻烦,去网上查的时候发现了用stl中map做这题的代码,觉得很简洁,而且自己也还没用过map,所以也算是一种新的认识。

思路:map的键存账号,值存密码。

代码如下:

#include <iostream>
#include <string>
#include <map>
using namespace std;
int main()
{
    int n;
    map<string, string>m;
    cin >> n;
    while(n--)
    {
        char c;
        string s1, s2;
        cin >> c >> s1 >> s2;
        if(c == 'N')
        {
            if(m.find(s1) != m.end()) cout << "ERROR: Exist\n";
            else
            {
                m[s1] = s2;
                cout << "New: OK\n";
            }
        }
        else
        {
            if(m.find(s1) == m.end()) cout << "ERROR: Not Exist\n";
            else if(m[s1] == s2) cout << "Login: OK\n";
            else cout << "ERROR: Wrong PW\n";
        }
    }
    return 0;
}

  • 2
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值