HDU 1080 map or trie树

传送门:HDU 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直接实现。
也可用trie树实现


AC code:

  1. map(用cin, cout tle换了scanf, printf。。。A了, 看来关了同步也不稳啊)
/*
 *adrui's submission
 *Language : C++
 *Result : Accepted
 *Favorite : Dragon Balls
 *Love : yy
 *
 *Standing in the Hall of Fame
 */
#include<algorithm>
#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<string>
using namespace std;

#define debug 0
#define M(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f


int main(){
#if debug
    freopen("in.txt", "r", stdin);
#endif // debug

    cin.tie(0);
    cin.sync_with_stdio(false);

    int s;
    int n;

    while(~scanf("%d", &n)){
        map<int, int>q;
        int ans = -inf;
        for(int i = 0; i < n; ++i){
            scanf("%d", &s);
            ++q[s];
            ans = max(ans, q[s]);
        }

        printf("%d\n", ans);
    }
    return 0;
}
  1. trie
/*
 *adrui's submission
 *Language : C++
 *Result : Accepted
 *Favorite : Dragon Balls
 *Love : yy
 *
 *Standing in the Hall of Fame
 */
#include<algorithm>
#include<cmath>
#include<iostream>
#include<map>
#include<cstring>
using namespace std;

#define debug 0
#define M(a, b) memset(a, b, sizeof(a))
#define inf 0x3f3f3f3f

const int maxn(9e5 + 5);
int ch[maxn * 10][10], v[maxn];
struct trie{
    int sz, ans;

    trie(){
        sz = 1;
        M(cnt, 0);
        M(ch, 0);
        ans = -inf;
    }

    void insert(char *s){
        int len = strlen(s), u = 0;
        int idx = 0;
        while(s[idx] == '0') ++idx;

        for(int i = idx; i < len; ++i){

            int c = s[i] - '0';
            if(!ch[u][c]){
                ch[u][c] = sz++;
            }
            u = ch[u][c];
        }

        ++cnt[u];
        ans = max(ans, cnt[u]);
    }
};

int main(){
#if debug
    freopen("in.txt", "r", stdin);
#endif // debug

    cin.tie(0);
    cin.sync_with_stdio(false);

    char s[35];
    int n;

    while(cin >> n){

        trie rt;
        for(int i = 0; i < n; ++i){
            cin >> s
            rt.insert(s);
        }

        cout << rt.ans << endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值