10
boar
pig
sheep
gazelle
sheep
sheep
alpaca
alpaca
marmot
mole
sheep 3
很明显的 字典树问题 看起来好看 但是 不好用呀....
1 /*以后就要独立完成了 已经知道了思想 */ 2 #include<stdio.h> 3 #include<string.h> 4 #include<malloc.h> // 好久都不用 忘了.... 5 struct node 6 { 7 int sum; //用于计数 8 node *next[26]; // 接下来的 有相同的 26个节点 9 }; 10 int search (char *s,node *T) 11 { 12 int i,j,id; 13 node *p,*q; 14 p=T; 15 i=0; 16 while(s[i]) 17 { 18 id=s[i]-‘a‘; 19 if(p->next[id]==NULL) 20 { 21 q=(node *)malloc(sizeof(node)); 22 q->sum=0; 23 for(j=0;j<26;++j) 24 q->next[j]=NULL; 25 p->next[id]=q; 26 } 27 p=p->next[id]; 28 ++i; 29 } // 在单词的 姐位数 开始 计数 30 (p->sum)++; 31 return p->sum; //返回 计数 32 } 33 int main() 34 { 35 int n,i,num,max; 36 char temp[50]; 37 char name[50]; 38 while(scanf("%d",&n)!=EOF) //输入 动物的数量 39 { 40 node *T; // 申请一个 头结点 T 41 T=(node *)malloc(sizeof(node)); //申请空间 42 T->sum=0; // 数量置零 43 for(i=0;i<26;++i) 44 T->next[i]=NULL; // 子树为空 45 max=0; 46 while(n--) // 说好了 N 个 动物 47 { 48 scanf("%s",temp); // 输入第一个动物的名字 49 num=search(temp,T); // 传送过去 动物的名字插入树中 并且返回 该动物的数量 50 if(num>max) 51 { 52 max=num; 53 strcpy(name,temp); 54 } 55 } 56 printf("%s %d\n",name,max); 57 } 58 return 0; 59 }
原文:http://www.cnblogs.com/A-FM/p/5181956.html