Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6271 Accepted Submission(s): 2053
1 /* 2 功能Function Description: HDOJ 1800 3 开发环境Environment: DEV C++ 4.9.9.1 4 技术特点Technique: 5 版本Version: 6 作者Author: 可笑痴狂 7 日期Date: 20120810 8 备注Notes: 9 题目意思: 10 有n个士兵每个人有一个水平值,水平高的的人可以教低的人,意思就是求最少的扫帚, 11 那么我们只要知道找到最大重复元素的次数即可,因为相同的人肯定不能共用一个, 12 所以求得最少即为最大的重复次数 13 注意:前置的0必须要去掉,例如数据 14 3 15 0 16 00 17 000 18 输出 3 19 20 这题和拦截导弹那题(动态规划求最长不降子序列)的区别是 导弹来的顺序是固定的不能改变,而这道题是可以随意变动的 21 */ 22 23 /* 24 //代码一:-------直接排序找最长平台(贪心算法) 25 #include<cstdio> 26 #include<cstring> 27 #include<algorithm> 28 using namespace std; 29 30 int main() 31 { 32 int n,i,max,t; 33 int a[3005]; 34 while(scanf("%d",&n)!=EOF) 35 { 36 for(i=0;i<n;++i) 37 scanf("%d",&a[i]); 38 sort(a,a+n); 39 max=1; 40 t=1; 41 for(i=1;i<n;++i) 42 { 43 if(a[i]!=a[i-1]) 44 t=1; 45 else 46 ++t; 47 if(t>max) 48 max=t; 49 } 50 printf("%d\n",max); 51 } 52 return 0; 53 } 54 55 56 //代码二:---------字典树 57 #include<cstdio> 58 #include<cstring> 59 #include<stdlib.h> 60 61 struct node 62 { 63 int sum; 64 node *next[26]; 65 }; 66 node memory[1000000]; 67 int k; 68 int max; 69 70 void insert(char *t,node *T) 71 { 72 int i,id,j; 73 node *p,*q; 74 p=T; 75 i=0; 76 while(t[i]) 77 { 78 id=t[i]-'0'; 79 if(p->next[id]==NULL) 80 { 81 // q=(node *)malloc(sizeof(node)); 82 q=&memory[k++]; 83 q->sum=0; 84 for(j=0;j<26;++j) 85 q->next[j]=NULL; 86 p->next[id]=q; 87 } 88 p=p->next[id]; 89 ++i; 90 } 91 p->sum++; 92 if(max<p->sum) 93 max=p->sum; 94 } 95 96 int main() 97 { 98 int n,i; 99 char s[35]; 100 node *T; 101 // T=(node *)malloc(sizeof(node)); 102 T=&memory[0]; 103 while(scanf("%d",&n)!=EOF) 104 { 105 max=0; 106 k=1; 107 T->sum=0; 108 for(i=0;i<26;++i) 109 T->next[i]=NULL; 110 while(n--) 111 { 112 scanf("%s",s); 113 i=0; 114 while(s[i++]=='0'); 115 insert(&s[i-1],T); 116 } 117 printf("%d\n",max); 118 } 119 return 0; 120 } 121 122 */ 123 124 //代码三: 125 126 //BST代码:----二分查找树/二叉排序树 127 128 #include <stdio.h> 129 #include <malloc.h> 130 struct node 131 { 132 int num; 133 node *lc; 134 node *rc; 135 int time; 136 }; 137 int max; 138 struct node *BST(struct node *root,int num) 139 { 140 if (root==NULL) //根节点为空 建立根节点 141 { 142 root=(struct node *)malloc(sizeof(struct node)); 143 root->lc=NULL; 144 root->rc=NULL; 145 root->num=num; 146 root->time=1; 147 } 148 else 149 { 150 if (root->num==num) // 根的name 和 s相同,次数+1 151 { 152 root->time++; 153 if (root->time>max) 154 max=root->time; 155 } 156 else if (root->num>num) // 根比s大,找左子树 157 root->lc=BST(root->lc,num); 158 else // 根比s小,找右子树 159 root->rc=BST(root->rc,num); 160 } 161 return root; 162 } 163 int main() 164 { 165 int n,i,num; 166 while (scanf("%d",&n)!=EOF) 167 { 168 struct node *root=NULL; 169 max=1; 170 for (i=0;i<n;i++) 171 { 172 scanf("%d",&num); 173 root=BST(root,num); 174 } 175 printf ("%d\n",max); 176 } 177 return 0; 178 }