zoukankan      html  css  js  c++  java
  • hdu--1800--字典树&&其他

    题目连接: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
    ",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
    ",maxx);
    	}
    	return 0;
    }


     

  • 相关阅读:
    HDU 1165 Eddy's research II (推公式)
    HDU 1394 Minimum Inversion Number (线段树&&暴力)
    HDU 2845 Beans (最大不连续子序列和)
    CodeForces 369A Valera and Plates( 水)
    HDU 1241 Oil Deposits(dfs)
    hdu 1016 Prime Ring Problem(dfs)
    hdu 5138 CET-6 test(水)
    ZOJ 3693 Happy Great BG(卡精度)
    HDU 1028 Ignatius and the Princess III(dp 母函数)
    CodeForces 432B Football Kit(水)
  • 原文地址:https://www.cnblogs.com/riskyer/p/3253543.html
Copyright © 2011-2022 走看看