QQ帳戶的申請與登陸


QQ帳戶的申請與登陸

實現QQ新帳戶申請和老帳戶登陸的簡化版功能。最大挑戰是:據說現在的QQ號碼已經有10位數了。

輸入格式:

輸入首先給出一個正整數N(≤ 105),隨后給出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

 

解題思路

  先給出std::unordered_map實現的AC代碼:

 1 #include <iostream>
 2 #include <vector>
 3 #include <string>
 4 #include <unordered_map>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 int main() {
 9     int n;
10     cin >> n;
11     unordered_map<string, string> mp;
12     
13     for (int i = 0; i < n; i++) {
14         string op, id, pw;
15         cin >> op >> id >> pw;
16         if (op == "N") {
17             if (mp.count(id) == 1) {
18                 cout << "ERROR: Exist\n";
19             }
20             else {
21                 mp[id] = pw;
22                 cout << "New: OK\n";
23             }
24         }
25         else if (op == "L") {
26             if (mp.count(id) == 0) {
27                 cout << "ERROR: Not Exist\n";
28             }
29             else if (mp[id] == pw) {
30                 cout << "Login: OK\n";
31             }
32             else {
33                 cout << "ERROR: Wrong PW\n";
34             }
35         }
36     }
37     
38     return 0;
39 }

  然后是手寫的哈希散列AC代碼:

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <vector>
 6 
 7 const int MAXN = 17;
 8 
 9 struct Data {
10     char id[MAXN], pw[MAXN];
11 };
12 
13 struct HashTable {
14     int tableSize;
15     std::vector<std::vector<Data> > table;
16 };
17 
18 HashTable *createTable();
19 int prime();
20 bool find(Data &data, HashTable *ht);
21 void insert(Data &data, HashTable *ht);
22 
23 int main() {
24     int n;
25     scanf("%d", &n);
26     HashTable *ht = createTable();
27     
28     for (int i = 0; i < n; i++) {
29         char op;
30         scanf("\n%c", &op);
31         Data data;
32         scanf(" %s %s", data.id, data.pw);
33         
34         if (op == 'N') {
35             if (!find(data, ht)) {
36                 insert(data, ht);
37                 puts("New: OK");
38             }
39             else {
40                 puts("ERROR: Exist");
41             }
42         }
43         else if (op == 'L') {
44             if (!find(data, ht)) {
45                 puts("ERROR: Not Exist");
46             }
47             else {
48                 int pos = atoi(data.id) % 10000;
49                 std::vector<Data>::iterator it = ht->table[pos].begin();
50                 for ( ; strcmp(it->id, data.id); it++);
51                 if (strcmp(it->pw, data.pw) == 0) puts("Login: OK");
52                 else puts("ERROR: Wrong PW");
53             }
54         }
55     }
56     
57     return 0;
58 }
59 
60 HashTable *createTable() {
61     HashTable *ht = new HashTable;
62     ht->tableSize = prime();
63     ht->table.resize(ht->tableSize);
64     
65     return ht;
66 }
67 
68 int prime() {
69     int i = 10001;
70     while (true) {
71         int j = (int)sqrt(10000);
72         for ( ; j > 2; j--) {
73             if (i % j == 0) break;
74         }
75         if (j == 2) break;
76         i += 2;
77     }
78     
79     return i;
80 }
81 
82 bool find(Data &data, HashTable *ht) {
83     int pos = atoi(data.id) % 10000;
84     for (std::vector<Data>::iterator it = ht->table[pos].begin(); it != ht->table[pos].end(); it++) {
85         if (strcmp(it->id, data.id) == 0) return true;
86     }
87     
88     return false;
89 }
90 
91 void insert(Data &data, HashTable *ht) {
92     int pos = atoi(data.id) % 10000;
93     ht->table[pos].push_back(data);
94 }

  手寫實現一般會比STL的要快,比如還有手寫堆和優先隊列。如果題目時間不是卡的很緊,還是用STL吧,畢竟太方便了。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM