HDU 1800 Flying to the Mars

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1800

/* http://acm.hdu.edu.cn/showproblem.php?pid=1800 Flying to the Mars next[] 也就是数字 0-9 */
#include<cstdio>
#include<cstring>
#include<stdlib.h>

typedef struct node{
    int count;
    struct node* next[11];
    node(int _count = 0)
    {
        count = _count;
        int i;
        for (i = 0; i < 11; i++)
        {
            next[i] = NULL;
        }
    }
}trie;

int max;

void insert(trie *T, char* s) 
{
    int i = 0;
    trie* p = T;
    trie* q;
    while (s[i] != '\0')
    {
        int id = s[i] - '0';
        if (p->next[id] == NULL)
        {
            q = new node(0);
            p->next[id] = q;
        }
        p = p->next[id]; // p 跳到下一个
        i++;
    }
    p->count++; // p 为 最后的定位 数量加加
    if (p->count > max)
        max = p->count;
}

int main()
{
    //freopen("in", "r", stdin);
    int n, i;
    char s[35];
    trie* T;
    while (scanf("%d", &n) != EOF)
    {
        T = new node(0);
        max = 0;
        while (n--)
        {
            scanf("%s", s);
            i = 0;
            while (s[i++] == '0'); // 清除前面的0
            insert(T , &s[i-1]); // & 取地址
        }
        printf("%d\n", max);
    }
    return 0;
}

你可能感兴趣的:(HDU 1800 Flying to the Mars)