POJ1002的懶人做法。。。

來源:互聯網
上載者:User

這道題的題意就是如果遇到相同的號碼就輸出,這樣用STL中的map加上string可以寫的很簡單,但是結果就不是很好了。

好囧的時間,2000ms的題1954ms卡過,在代碼中的map可以用離散化hash代替,可以大大地最佳化代碼。

 

My Code思路就是讀取字串,hash,然後對應的映射加1,最後統計大於等於2的全部輸出即可,cnt計數器,當cnt等於0的時候輸出No duplicates.之前漏了這個,WA了兩次。。。

 

#include <iostream><br />#include <cstdio><br />#include <cstring><br />#include <cstdlib><br />#include <string><br />#include <map></p><p>using namespace std;</p><p>const char hash[100]="22233344455566677778889999";</p><p>int main()<br />{<br />int n,cnt,len;<br />string ret;<br />char str[100];<br />map<string,int> mp;</p><p>scanf("%d",&n);<br />while(n--)<br />{<br />scanf("%s",str);<br />cnt=0;<br />ret="";<br />len=strlen(str);<br />for(int i=0;i<len;i++)<br />{<br />if(str[i]>='0'&&str[i]<='9')<br />{<br />ret+=str[i];<br />cnt++;<br />if(cnt==3)<br />ret+='-';<br />}<br />else if(str[i]>='A'&&str[i]<'Z'&&str[i]!='Q')<br />{<br />ret+=hash[str[i]-'A'];<br />cnt++;<br />if(cnt==3)<br />ret+='-';<br />}<br />if(cnt==7)<br />break;<br />}<br />mp[ret]++;<br />}</p><p>cnt=0;<br />map<string,int>::iterator it;<br />for(it=mp.begin();it!=mp.end();it++)<br />{<br />if((*it).second>1)<br />{<br />printf("%s %d/n",(*it).first.c_str(),(*it).second);<br />cnt++;<br />}<br />}</p><p>if(cnt==0)<br />printf("No duplicates./n");</p><p>return 0;<br />}<br />

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.