題目串連:http://acm.hdu.edu.cn/showproblem.php?pid=1800
根據題意可知:意思是有若干個飛行員,需要在掃帚上練習飛行,每個飛行員具有不同的等級,且等級高的飛行員可以當等級低的飛行員的老師,且每個飛行員至多有且只有一個老師和學生。具有老師和學生關係的飛行員可以在同一把掃帚上練習,並且這個性質具有傳遞性。即比如有A,B,C,D,E五個飛行員,且等級是A>B>C>D>E,那麼可以使A當B的老師,B當C的老師,E當D的老師,那麼A,B,C可以在同一掃帚上練習,D,E在同一把掃帚上練習,這樣需要2把掃帚,而如果是A當B的老師,B當C的老師,C當D的老師,D當E的老師,那麼只需要一把掃帚。題目所求即所需最少的掃帚數目。
假設有若干個飛行員,{{A1,A2,A3...AK},{B1,B2,B3,...Bm}......{F1,F2,F3.....Fn}}。其已經按照等級由低到高排好序,在同一個集合裡的飛行員等級相同。若需要最少數目的掃帚,則只能是{A1,B1.....F1},{A2,B2....F2}..這樣進行組合,掃帚數目最少。因此決定所需最少掃帚數目的集合是含有飛行員最多的集合,即同一等級數目最多的飛行員集合。因此可以採用STL中的map直接實現。
代碼:
/*hdu 1800 最大分配 */ #include <iostream>#include<map>using namespace std;int main(){ int n; while(scanf("%d",&n)==1) { int i; map<int,int> mp; int max=0; for(i=0;i<n;i++) { int level; scanf("%d",&level); mp[level]++; if(mp[level]>max) { max=mp[level]; } } printf("%d\n",max); } return 0;}
字典樹:
#include <iostream>#include<malloc.h>#include<string.h>using namespace std;typedef struct node{int count;node* next[10];}trie;trie* root;char s[35];int maxx;trie* New(){trie* p;p=(trie *)malloc(sizeof(trie));for(int i=0;i<10;i++){p->next[i]=NULL;//p->next[i]->count =0;}p->count=0;return p;}void Insert(char *s){trie* p=root;int len=strlen(s);int j=0;while(s[j]=='0'){j++;}for(int i=j;i<len;i++){int x=s[i]-'0';if(p->next[x]==NULL){p->next[x]=New();}p=p->next[x];}p->count ++;if(p->count >maxx)maxx=p->count ; }int main(){int n;while(scanf("%d",&n)!=EOF){maxx=0;root=New();for(int i=0;i<n;i++){scanf("%s",s);Insert(s);}printf("%d\n",maxx);}return 0;}
這個。。。。用C++提交才對。。。
/*ÌâÒ⣺ÊäÈëÒ»¸öÕûÊýn£¬ÏÂÃæÊäÈënÐвâÊÔÊý¾Ý£¬Ã¿ÐÐÊÇÒ»¸öÕûÊý£¨Ö»º¬Êý×ÖµÄ×Ö·û´££¬Êä³öÏàͬÕûÊýµÄ×î¶à´ÎÊý01,001,0001ÊôÓÚÏàͬµÄÕûÊý */#include<stdio.h>#include<stdlib.h>#include<string.h>//using namespace std;int max;char s[50];typedef struct node{int count;struct node *next[10];}tree;tree *head;void init(){int i;head=(tree *)malloc(sizeof(tree));head->count=0;for(i=0; i<10; i++){head->next[i]=NULL;}}int insert(char *s){tree *q,*p;p=head;int i,j;int len=strlen(s);int k=0; while(s[k]=='0') k++;for(i=k; i<len; i++){if(p->next[s[i]-'0']==NULL){q=(tree*)malloc(sizeof(tree));p->next[s[i]-'0']=q;p=p->next[s[i]-'0'];p->count=0;for(j=0; j<10; j++){p->next[j]=NULL;}}else{p=p->next[s[i]-'0'];}}p->count++;if(p->count>max){max=p->count;}return max;}int main(){int n,i,j;while(scanf("%d",&n)!=EOF){ init(); int num=0; int maxn=0; max=0;for(i=0;i<n;i++){scanf("%s",s);num=insert(s);if(num>maxn){maxn=num;}}printf("%d\n",num);}return 0;}