标签:
Description
Input
Output
Sample Input
4 10 20 30 04 5 2 3 4 3 4
Sample Output
1 2
1 #include<cstdio> 2 #include<cstring> 3 #include<cstdlib> 4 typedef struct Trie 5 { 6 int num; 7 struct Trie *next[10]; 8 }Node,*trie; 9 int ans; 10 Node *create() 11 { 12 Node *node=(Node *)malloc(sizeof(Node)); 13 node->num=0; 14 memset(node->next,0,sizeof(node->next)); 15 return node; 16 } 17 void insert_(trie root,char *str) 18 { 19 trie node=root; 20 char *p=str; 21 int id; 22 while(*p) 23 { 24 id=*p-‘0‘; 25 if(node->next[id]==NULL) 26 node->next[id]=create(); 27 node=node->next[id]; 28 ++p; 29 } 30 node->num+=1; 31 if(node->num>ans) ans=node->num; 32 } 33 int deal(trie T) 34 { 35 if(T==NULL) 36 return 0; 37 for(int i=0;i<10;i++) 38 { 39 if(T->next[i]!=NULL) 40 deal(T->next[i]); 41 } 42 free(T); 43 return 0; 44 } 45 int main() 46 { 47 char str[33]; 48 int n; 49 while(scanf("%d",&n)!=EOF) 50 { 51 trie root=create(); 52 ans=0; 53 while(n--) 54 { 55 char c[33]; 56 scanf("%s",str); 57 for(int i=0;str[i];i++) 58 if(str[i]!=‘0‘||!str[i+1]) 59 { 60 strcpy(c,&str[i]); 61 break; 62 } 63 insert_(root,c); 64 } 65 printf("%d\n",ans); 66 deal(root); 67 } 68 return 0; 69 }
标签:
原文地址:http://www.cnblogs.com/PrayG/p/5899656.html