hdu 1800 Flying to the Mars

简介: 点击打开hdu 1800 题意:有n个士兵每个人有一个能力值d,现在士兵要去学习如何飞到火星。规定如下,能力值大的可以教能力值小的并且每个人只能由一个人来教,而且每个人只能够教一个人。

点击打开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;
}



目录
相关文章
|
11月前
|
算法 C++
【PAT甲级 - C++题解】1044 Shopping in Mars
【PAT甲级 - C++题解】1044 Shopping in Mars
57 0
|
11月前
|
C++
【PAT甲级 - C++题解】1027 Colors in Mars
【PAT甲级 - C++题解】1027 Colors in Mars
48 0
HDU-1846,Brave Game(巴什博弈)
HDU-1846,Brave Game(巴什博弈)
|
Python
Mars 是什么、能做什么、如何做的——记 Mars 在 PyCon China 2018 上的分享
最近,在 PyCon China 2018 的北京主会场、成都和杭州分会场都分享了我们最新的工作 Mars,基于矩阵的统一计算框架。本文会以文字的形式对 PyCon 中国上的分享再进行一次阐述。 听到 Mars,很多第一次听说的同学都会灵魂三问:Mars 是什么,能做什么,怎么做的。
4850 0
1100. Mars Numbers (20)
#include #include using namespace std; string mars0[14] = {"tret", "jan", "feb", "mar", "apr", "may", "jun"...
925 0
|
ice 机器学习/深度学习 移动开发