日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

pta-HASH--QQ账号与密码注册和登录

發(fā)布時(shí)間:2024/3/24 43 豆豆
生活随笔 收集整理的這篇文章主要介紹了 pta-HASH--QQ账号与密码注册和登录 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

輸入格式:

輸入首先給出一個(gè)正整數(shù)N),隨后給出N行指令。每行指令的格式為:“命令符(空格)QQ號(hào)碼(空格)密碼”。其中命令符為“N”(代表New)時(shí)表示要新申請(qǐng)一個(gè)QQ號(hào),后面是新帳戶的號(hào)碼和密碼;命令符為“L”(代表Login)時(shí)表示是老帳戶登陸,后面是登陸信息。QQ號(hào)碼為一個(gè)不超過10位、但大于1000(據(jù)說QQ老總的號(hào)碼是1001)的整數(shù)。密碼為不小于6位、不超過16位、且不包含空格的字符串。

輸出格式:

針對(duì)每條指令,給出相應(yīng)的信息:

1)若新申請(qǐng)帳戶成功,則輸出“New: OK”;2)若新申請(qǐng)的號(hào)碼已經(jīng)存在,則輸出“ERROR: Exist”;3)若老帳戶登陸成功,則輸出“Login: OK”;4)若老帳戶QQ號(hào)碼不存在,則輸出“ERROR: Not Exist”;5)若老帳戶密碼錯(cuò)誤,則輸出“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

這道題實(shí)際上O(n√ ̄n),688ms左右,hash表傳言O(shè)(1)查找,實(shí)際上不夠穩(wěn)定,我寫的hash運(yùn)行時(shí)間和map差不多。。。


1.線性表:平方探測(cè)處理沖突


#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxm=99997;//比表長(zhǎng)10^5小的質(zhì)數(shù) const int maxn=110000;//平方探測(cè)法n太小容易找不到 !!!!一定要覆蓋全部數(shù),大于n也要大于999999999%99997char op;//操作 struct p {ll qq;char ma[20];}; p hash1[maxn];//hash表 int DealConflict(p t,int index)//平方探測(cè)法處理沖突 {int temp;for(int i=0;i<=sqrt(maxn)+1;i++){if(i%2==1)//奇數(shù)加 {temp=index+(i/2+1)*(i/2+1);if(temp<maxn)//不越界 {if(hash1[temp].qq==0){return temp;}}}else//偶數(shù)減 {temp=index-(i/2)*(i/2);if(temp>=0)//不越界 {if(hash1[temp].qq==0){return temp;}} }}}void CreateHash(p t)//取模法建立hash {int temp=t.qq%maxm;int set=DealConflict(t,temp);hash1[set].qq=t.qq;strcpy(hash1[set].ma,t.ma);}int SearchHash(p t)//查找qq號(hào) 密碼匹配 {ll index=t.qq%maxm;int cur;for(int i=0;i<=sqrt(maxn)+1;i++){if(i%2==1)//奇數(shù)加 {cur=index+(i/2+1)*(i/2+1);if(cur<maxn)//不越界 {if(hash1[cur].qq==t.qq){if(strcmp(hash1[cur].ma,t.ma)==0)return 2;elsereturn 1;}}}else//偶數(shù)減 {cur=index-(i/2)*(i/2);if(cur>=0)//不越界 {if(hash1[cur].qq==t.qq){if(strcmp(hash1[cur].ma,t.ma)==0)return 2;elsereturn 1;}} }}return 0; }int main() { int n; cin>>n; p temp; while(n--) {cin>>op>>temp.qq>>temp.ma;if(op=='N'){if(SearchHash(temp)==1||SearchHash(temp)==2){cout<<"ERROR: Exist"<<endl;}else{cout<<"New: OK"<<endl;CreateHash(temp);}}else{if(SearchHash(temp)==0){cout<<"ERROR: Not Exist"<<endl;}else{if(SearchHash(temp)==1){cout<<"ERROR: Wrong PW"<<endl;}//elsecout<<"Login: OK"<<endl;}}}}

2.鏈表:類似鄰接表的結(jié)構(gòu)(但不帶頭結(jié)點(diǎn),因?yàn)槌跏蓟^結(jié)點(diǎn)需要時(shí)間)

#include<bits/stdc++.h> using namespace std; typedef long long ll; const int maxm=99997;//比表長(zhǎng)10^5小的質(zhì)數(shù)? const int maxn=110000; vector<int>p; char op; struct q1 {ll qq;char ma[20];};struct q2 {ll qq;char ma[20];q2 *next;}Q; q2 *hash1[maxn];//void Init() //{ // hash1[cur]=new q2;// ?hash1[cur]->next=NULL; //} void Insert(q1 t)? {int cur=t.qq%maxm;q2 *s;s=new q2;s->qq=t.qq;strcpy(s->ma,t.ma);s->next=NULL;s->next=hash1[cur];hash1[cur]=s;}int Search(q1 t) {int cur=t.qq%maxm;if(hash1[cur]==NULL)//帶頭結(jié)點(diǎn)的話,初始化全部時(shí)間耗費(fèi)大,所以不帶頭節(jié)點(diǎn)?return 0;q2 *s;s=hash1[cur];while(s!=NULL){if(s->qq==t.qq){if(strcmp(s->ma,t.ma)==0)return 2;elsereturn 1;}s=s->next;}//if(s==NULL)//空要馬上跳出,不然下面報(bào)錯(cuò)? // return -1;return 0; }int main() {int n;cin>>n;q1 temp;for(int i=0;i<n;i++){cin>>op>>temp.qq>>temp.ma;if(op=='N'){if(Search(temp)==1||Search(temp)==2)cout<<"ERROR: Exist"<<endl;else?{cout<<"New: OK"<<endl;if(Search(temp)==0)Insert(temp);}}else{if(Search(temp)==0)cout<<"ERROR: Not Exist"<<endl;else if(Search(temp)==2)cout<<"Login: OK"<<endl;elsecout<<"ERROR: Wrong PW"<<endl;}}}

總結(jié)

以上是生活随笔為你收集整理的pta-HASH--QQ账号与密码注册和登录的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。