hdu--1800--字典树&&其他

来源:互联网 发布:php判断来源域名 编辑:程序博客网 时间:2024/05/21 06:56

题目连接: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;}



 

原创粉丝点击