NYOJ 286 动物统计
生活随笔
收集整理的這篇文章主要介紹了
NYOJ 286 动物统计
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
動(dòng)物統(tǒng)計(jì)
時(shí)間限制:1000?ms ?|? 內(nèi)存限制:65535?KB 難度:2 描述????? 在美麗大興安嶺原始森林中存在數(shù)量繁多的物種,在勘察員帶來的各種動(dòng)物資料中有未統(tǒng)計(jì)數(shù)量的原始動(dòng)物的名單。科學(xué)家想判斷這片森林中哪種動(dòng)物的數(shù)量最多,但是由于數(shù)據(jù)太過龐大,科學(xué)家終于忍受不了,想請聰明如你的ACMer來幫忙。
輸入樣例輸出
sheep 3
法三:字典樹。 #include<stdio.h> #include<string.h> #include<stdlib.h> struct node {int num;struct node *next[26]; }; struct node *root; node *build() {struct node *p=(node *)malloc(sizeof(node));p->num=1;for(int i=0;i<26;i++)p->next[i]=NULL;return p; } int insert(char *s) {struct node *p=root;int len=strlen(s);for(int i=0;i<len;i++){if(p->next[s[i]-'a']==NULL)p->next[s[i]-'a']=build();p=p->next[s[i]-'a']; }return p->num++; } int main() {int n,i,max=-1,a;char str[10],s[10];root=build();scanf("%d",&n);for(i=0;i<n;i++){scanf("%s",str);a=insert(str);if(a>max){max=a;strcpy(s,str); }}printf("%s %d\n",s,max);return 0; }
法一和法二只能在數(shù)據(jù)比較少的情況下使用,如果數(shù)據(jù)很多,則要用字典樹來解,不然會(huì)超時(shí)。
總結(jié)
以上是生活随笔為你收集整理的NYOJ 286 动物统计的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Java 面试题全梳理
- 下一篇: NoSQL架构实践(三)——以NoSQL