HDU 1800 Flying to the Mars -- 求一组数中出现频率最大的那个数出现的次数

来源:互联网 发布:在线视频转码 知乎 编辑:程序博客网 时间:2024/05/17 01:58

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

题意:有N个士兵,每个士兵有一个属于自己的等级,他们要学一项魔杖飞行技术,等级高的士兵可以教等级低的士兵,等级低的士兵不可以教等级高的士兵,一个士兵只能教一个士兵,一个士兵也只能被一个士兵教,能形成这样的教学关系的士兵组成一个教学组,需要一根魔杖,问最少需要多少根魔杖。即求
求一组数中出现频率最大的那个数出现的次数!
可以用hash,也可以用字典树

/*http://acm.hdu.edu.cn/showproblem.php?pid=1800Flying to the Marsnext[] 也就是数字 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;}
0 0