hdu 1800 Flying to the Mars

点击打开hdu 1800

题意:有n个士兵每个人有一个能力值d,现在士兵要去学习如何飞到火星。规定如下,能力值大的可以教能力值小的并且每个人只能由一个人来教,而且每个人只能够教一个人。规定一起学习的人的书本是一样的,问最少需要几本书

思路:根据题目的意思是我们可以知道最终要求的就是有几个递减的序列,也就是找到最多重复的值。比如2 3 4 3 4 就是两个递减序列4 3 2 和 4 3。那么我们只要对这些的值插入到字典树即可,利用字典树的性质找到最多重复的个数。但是要注意前缀,在字典树中001和01是不同的,但是我们应该要把前缀给去掉。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAXN = 3010;
const int N = 35;
const int M = 10;

struct Node{
    int num;
    Node* child[M];
};
Node node[MAXN*N];
Node* root;
int ans , n , pos;

Node* newNode(){
    node[pos].num = 0;
    for(int i = 0 ; i < M ; i++)
        node[pos].child[i] = NULL;
    return &node[pos++];
}

void insert(char *str){
    Node* s = root;
    int len = strlen(str);
    for(int i = 0 ; i < len ; i++){
        int num = str[i]-'0';
        if(s->child[num] == NULL)
            s->child[num] = newNode();
        s = s->child[num];
    }
    s->num++;
    ans = max(ans , s->num);
}

int main(){
    char str[N];
    while(scanf("%d" , &n) != EOF){
         pos = 0;
         ans = 0;
         root = newNode();
         while(n--){
              scanf("%s" , str);
              int j = 0;
              while(str[j] == '0')
                  j++;
              insert(str+j);
         }
         printf("%d\n" , ans);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值