NTOJ 290 動物統計(加強版)

來源:互聯網
上載者:User

標籤:

動物統計加強版   
描述
在美麗大興安嶺原始森林中存在數量繁多的物種,在勘察員帶來的各種動物資料中有未統計數量的原始動物的名單。科學家想判斷這片森林中哪種動物的數量最多,但是由於資料太過龐大,科學家終於忍受不了,想請聰明如你的ACMer來幫忙。
 
輸入
第一行輸入動物名字的數量N(1<= N <= 4000000),接下來的N行輸入N個字串表示動物的名字(字串的長度不超過10,字串全為小寫字母,並且只有一組測試資料)。 
輸出
輸出這些動物中最多的動物的名字與數量,並用空格隔開(資料保證最多的動物不會出現兩種以上)。 
範例輸入
10boarpigsheepgazellesheepsheepalpacaalpacamarmotmole
範例輸出
sheep 3


 1 #include<cstdio> 2 #include<cstring> 3 #include<string> 4 #include<iostream> 5 #include<algorithm> 6 using namespace std; 7  8 struct trie 9 {10     trie *next[26];11     int v;12     trie()13     {14         memset(next,0,sizeof(next));15         v=0;16     }17 };18 19 int ans;20 string ans_s;21 22 trie *root=new trie;23 24 void creatrie(char *s)25 {26     trie *p=root;27     int len=strlen(s);28     for(int i=0;i<len;i++)29     {30         int id=s[i]-‘a‘;31         if(p->next[id]==NULL)32         {33             trie *q=new trie;34             p->next[id]=q;35             p=p->next[id];36         }37         else38         {39             p=p->next[id];40         }41         if(i==len-1)42         {43             p->v+=1;44             if(p->v>ans)45             {46                 ans_s=s;47                 ans=p->v;48             }49         }50     }51 }52 53 int main()54 {55     int n;56     char s[11];57     scanf("%d",&n);58     while(n--)59     {60         scanf("%s",s);61         creatrie(s);62     }63     cout<<ans_s<<" "<<ans<<endl;64     return 0;65 }

 

NTOJ 290 動物統計(加強版)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.