hdu 1800 (字典树)

本文介绍了一种利用字典树(Trie)优化数列排序的问题解决方法,通过构建字典树来实现对输入数列的有效处理,使得能够找出数列中可以构成的最大数量的递减子序列。

题意:将输入的数由大到小排列(没有重复的),最少能排多少.

例如:1 3 4 5 2 3 4 。则能排两个分别是 (1 2 3 4 5)和(3 4)。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>

using namespace std;

typedef struct TrieNode
{
    int nm;
    struct TrieNode *child[10];

    TrieNode()
    {
        nm = 0;
        memset(child , 0, sizeof(child));
    }
}TNode;
TNode *head;
int res;

void insert(char *str)
{
    //TNode *head;

    TNode *p;
    p = head;
    int i = 0;
    while (str[i])
    {
        int id = str[i] - '0';
        if (!p->child[id])
        {
            p->child[id] = new TrieNode();
        }
        p = p->child[id];
        i++;
    }
    p->nm++;
    res = max(p->nm, res);
}

int main(void)
{
    int n;

    while (scanf("%d", &n) != EOF)
    {
        char str[32];
        res = 0;
        head = new TrieNode();
        int i;
        for (i=0; i<n; i++)
        {
            scanf("%s", str);
            int j = 0;
            while (str[j] == 0)
            {
                j++;
            }
            insert(str+j);
        }

        printf("%d\n", res);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/bucuo/archive/2013/03/21/2973561.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值