HDU1800 Flying to the Mars【字典树】

题目链接:

http://acm.hdu.edu.cn/showproblem.php?pid=1800

题目大意:

有N个士兵。用不同的整数表示不同的级别。级别高的士兵可以教级别低的士兵,他们可以共用一把

扫帚。一个士兵最多只能有一个学生或一个老师。问:最少需要几把扫帚。

思路:

对于士兵都不相同的士兵,只需要一把扫帚。那么问题转变为找出给出数理重复次数最多的个数。建

立字典树,将每个数当作字符串插入字典树中,记录每个数出现的次数,最后找出重复出现次数的最

大值即为所求。注意:04和4都表示4,插入的时候,应该清除掉前导零。

AC代码:

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

struct TrieNode
{
    int Count;
    struct TrieNode *Next[10];
};

TrieNode *root;
int Max;

void Create()
{
    root = new TrieNode;
    memset(root->Next,NULL,sizeof(root->Next));
    root->Count = 0;
}

void Insert(char *s)
{
    TrieNode *p, *q;
    p = root;
    while(*s)
    {
        if(!p->Next[*s-'0'])
        {
            q = new TrieNode;
            memset(q->Next,NULL,sizeof(q->Next));
            q->Count = 0;
            p->Next[*s-'0'] = q;
        }

        p = p->Next[*s-'0'];
        s++;
    }
    p->Count++; //只记录每个数的个数,不记录前缀出现次数
    if(p->Count > Max)
        Max = p->Count;
}

char str[33];
int main()
{
    int N;
    while(~scanf("%d",&N))
    {
        Max = 0;
        Create();
        for(int i = 0; i < N; ++i)
        {
            scanf("%s",str);
            int j = 0;
            while(str[j] == '0')
                j++;
            Insert(str+j);
        }
        printf("%d\n",Max);
    }

    return 0;
}
时间: 2024-11-13 08:08:47

HDU1800 Flying to the Mars【字典树】的相关文章

hdu---(1800)Flying to the Mars(trie树)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 11228    Accepted Submission(s): 3619 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul

HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树

http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> #include<stdio.h> using namespace std; struct node { int sum; node *next[10]; node() { sum=0; memset(next, NULL, sizeof(next)); }; }; int ans; char

HDU1800Flying to the Mars(字典树)

题目: Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the population growing , PPF needs to find more land for the newborns . Finally , PPF decides to attack Kscinow who ruling the Mars . Here the problem comes! How can

HDU1800 Flying to the Mars 【贪心】

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 10854    Accepted Submission(s): 3481 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popu

hdu1800 Flying to the Mars(字典树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 14340    Accepted Submission(s): 4572 Problem Description In the year 8888

字典树(Trie)

 字典树:又称为Trie,是一种用于快速检索的多叉树结构.Trie把要查找的关键词看作一个字符序列,并根据构成关键词字符的先后顺序构造用于检索的树结构:一棵m度的Trie树或者为空,或者由m棵m度的Trie树构成. 注意:和二叉查找树不同的是,其节点并非存储一个元素. 优点:1.利用公共内存,以达到节约内存的目的 2.根节点只存储其子树,不存储字母 3.每个节点代表的字母都不同 基本定义: typedef struct Node { struct Node*child[26]; int n;

Flying to the Mars HDU - 1800(字典树)

Flying to the Mars HDU - 1800 题目链接:https://vjudge.net/problem/HDU-1800 题目:在8888年,地球由PPF帝国统治.随着人口的增长,PPF需要为新生儿寻找更多的土地.最后,PPF决定攻击统治火星的Kscinow.问题来了!士兵怎么能到达火星? PPF召集他的士兵并询问他们的建议. “匆匆......”一名士兵回答. “闭嘴 !我是否必须提醒你,从这里到火星没有任何道路!“PPF回复道. “飞!”另一个答案. PPF笑道:“聪明的

HDU 1800 Flying to the Mars(字典树)

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 12767    Accepted Submission(s): 4048 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popu

*HDU1800字典树

Flying to the Mars Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 18240    Accepted Submission(s): 5877 Problem Description In the year 8888, the Earth is ruled by the PPF Empire . As the popul