您的位置:首页 > 其它

hdu 1800 Flying to the Mars

2013-11-02 10:08 330 查看
点击打开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
;
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: