hdu 1800 (字典树)

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

例如: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;
}

 

posted @ 2013-03-21 15:40  尔滨之夏  阅读(204)  评论(0编辑  收藏  举报