HNUSTOJ 1601:名字缩写
生活随笔
收集整理的這篇文章主要介紹了
HNUSTOJ 1601:名字缩写
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
1601: 名字縮寫
時(shí)間限制:?1 Sec??內(nèi)存限制:?128 MB提交:?288??解決:?80
[提交][狀態(tài)][討論版]
題目描述
?Noname老師有一個(gè)班的學(xué)生名字要寫,但是他太懶了,想少寫幾個(gè)字母。很快他發(fā)現(xiàn)這是可行的,例如下面的學(xué)生名單:
Davidson Davis Dixon Smith 可以縮寫為 David Davis Di S David 指明Davidson外,不可能是其他三位同學(xué)名字的前綴。S僅能代表Smith。在確保能無歧義指明同學(xué)的前提下,Noname老師總是希望使用最少的字母。輸入
?給定一系列名字,每個(gè)名字一行(不超過100行),名字僅含英文字母,名字長度不超過40,這些名字按字母升序排列, 任意兩個(gè)名字不相同而且一個(gè)名字不會(huì)正好是另一個(gè)名字的前綴。
輸出
?每行輸入對應(yīng)一行輸出,內(nèi)容為空格分開原來的名字和縮寫后的名字。
樣例輸入
Adams Andersen Anderson Carson Carter Carville Cooper Coply Smith Smythe Sorensen Sorenson Wynn樣例輸出
Adams Ad Andersen Anderse Anderson Anderso Carson Cars Carter Cart Carville Carv Cooper Coo Coply Cop Smith Smi Smythe Smy Sorensen Sorense Sorenson Sorenso Wynn W提示
來源
2014湖南科技大學(xué)校賽
可以用字典樹,但這題數(shù)據(jù)量較小,可以嘗試STL里的map來記錄前綴數(shù)量就可以; 代碼如下:| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | #include<iostream> #include<cstring> #include<cstdio> #include<string> #include<map> using?namespace?std; const?int?N?=?100?+?5; char?name[N][45]; map<string,int>?mp; void?Add(char?*ch){ ????int?len?=?strlen(ch); ????for(int?i=1;i<=?len;i++){ ????????char?c?=?ch[i]; ????????ch[i]?=?'\0'; ????????mp[ch]++; ????????ch[i]?=?c; ????} } void?solve(int?x){ ????char?ch[45]; ????strcpy(ch,name[x]); ????int?len?=?strlen(ch); ????for(int?i=1;i<=len;i++){ ????????char?c?=?ch[i]; ????????ch[i]?=?'\0'; ????????if(mp[ch]==1){printf("%s?%s\n",name[x],ch);return;} ????????ch[i]?=?c; ????} } int?main(){ ????std::ios::sync_with_stdio(false); ????int?cnt?=?0; ????while(scanf("%s",name[cnt])==1){ ????????Add(name[cnt++]); ????} ????for(int?i=0;i<cnt;i++){ ????????solve(i); ????} } |
轉(zhuǎn)載于:https://www.cnblogs.com/Pretty9/p/7347694.html
總結(jié)
以上是生活随笔為你收集整理的HNUSTOJ 1601:名字缩写的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: FragmentTabHost的应用
- 下一篇: MVC4 下DropDownList使用