HDU 1800 Flying to the Mars(贪心 MAP 字典树 HASH)

来源:互联网 发布:域名联想工具 编辑:程序博客网 时间:2024/04/29 16:42

Flying to the Mars

Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8608    Accepted Submission(s): 2808


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 the soldiers reach the Mars ? PPF convokes his soldiers and asks for their suggestions . “Rush … ” one soldier answers. “Shut up ! Do I have to remind you that there isn’t any road to the Mars from here!” PPF replies. “Fly !” another answers. PPF smiles :“Clever guy ! Although we haven’t got wings , I can buy some magic broomsticks from HARRY POTTER to help you .” Now , it’s time to learn to fly on a broomstick ! we assume that one soldier has one level number indicating his degree. The soldier who has a higher level could teach the lower , that is to say the former’s level > the latter’s . But the lower can’t teach the higher. One soldier can have only one teacher at most , certainly , having no teacher is also legal. Similarly one soldier can have only one student at most while having no student is also possible. Teacher can teach his student on the same broomstick .Certainly , all the soldier must have practiced on the broomstick before they fly to the Mars! Magic broomstick is expensive !So , can you help PPF to calculate the minimum number of the broomstick needed .
For example :
There are 5 soldiers (A B C D E)with level numbers : 2 4 5 6 4;
One method :
C could teach B; B could teach A; So , A B C are eligible to study on the same broomstick.
D could teach E;So D E are eligible to study on the same broomstick;
Using this method , we need 2 broomsticks.
Another method:
D could teach A; So A D are eligible to study on the same broomstick.
C could teach B; So B C are eligible to study on the same broomstick.
E with no teacher or student are eligible to study on one broomstick.
Using the method ,we need 3 broomsticks.
……

After checking up all possible method, we found that 2 is the minimum number of broomsticks needed.
 

Input
Input file contains multiple test cases.
In a test case,the first line contains a single positive number N indicating the number of soldiers.(0<=N<=3000)
Next N lines :There is only one nonnegative integer on each line , indicating the level number for each soldier.( less than 30 digits);
 

Output
For each case, output the minimum number of broomsticks on a single line.
 

Sample Input
410203004523434
 

Sample Output
12
 

Author
PPF@JLU
 

Recommend
lcy
题目意思是PPF想去为占领火星以获取更好的资源,只有借用哈利波特的扫帚才能飞过去,PPF的士兵都会做扫帚(只是质量不怎么好),但是因为扫帚的造价很高,只有等级技能高的士兵仅带一个学生(当然技能等级要比前面的要高),这个学生可以教一个人..一直这样循环去,直至没有可教的..这样的话就只要一个扫帚...如果不然,那就只有,每个人要造一把——找出最小的扫帚数.
因为数据有前导0,而且数据有点大(30位以下)..但是AC后才发现,数据很小.........
(用数组存上以后,升序排列找到相同数字出现的最大次数即可...用SCANF可以去前导0,CIN果断不行))
#include<iostream>#include<cstdio>#include<algorithm>#define N 3000using namespace std;int main(){    int n,a[N],b[N];    int max,i,j,k,t;    while(cin>>n)    {        for(i=0;i<n;i++)           scanf("%d",&a[i]);        sort(a,a+n);        max=j=1;        for(i=0;i<n-1;i++)        {            if(a[i]!=a[i+1])            {                if(j>max)max=j;                j=1;            }            else j++;        }        if(j>max)            max=j;       cout<<max<<endl;    }    return 0;}

(用MAP果断实现...)
#include<iostream>#include<cstdio>#include<map>using namespace std;int main(){    map<long long ,long long >mp;    long long n,i;    while(cin>>n)    {        mp.clear();       while(n--)       {            scanf("%lld",&i);             mp[i]++;       }          map<long long,long long>::iterator it;          long long max=0;          for(it=mp.begin();it!=mp.end();it++)             if(max<it->second) max=it->second;             cout<<max<<endl;    }    return 0;}


(字典树实现..这个题目应该是考的这个算法吧..)
#include <iostream>#include <cstring>#include <cstdio>#include <map>using namespace std;struct node{    int count;    node *next[15];    node():count(0)    {        memset(next,0,sizeof(next));    }};node *root;int maxx;void insert(char *str){    int l=strlen(str);    int i;    node *p=root;    for(i=0;i<l;i++)    {        if(p->next[str[i]-'0']==0)            p->next[str[i]-'0']=new node;        p=p->next[str[i]-'0'];    }    p->count++;}void travel(node *p){    if(p->count>maxx)        maxx=p->count;    int i;    for(i=0;i<=9;i++)    {        if(p->next[i])            travel(p->next[i]);    }}void de(node *p){    int i;    for(i=0;i<10;i++)        if(p->next[i])            de(p->next[i]);    delete p;}int main(){    int n;    while(scanf("%d",&n)!=EOF)    {        int i;        maxx=0;        root =new node;        char str[35];        for(i=0;i<n;i++)        {            scanf("%s",str);            int j;            for(j=0;j<(int)strlen(str);j++)                if(str[j]!='0')                    break;            insert(str+j);        }        travel(root);        printf("%d\n",maxx);        de(root);    }    return 0;}

(HASH表...这个也坚强)
/**  分析:题意要求找字符串个数最多的一个*  方法:很多,hash就是其中一种 *  注意两点:1.去掉前导0; 2.每组测试数据后,要释放内存 *  第一点解析:例如数据01,001,0001, 00001不去前导0的话,hash以后映射到不同的表位置 */#include <cstdio>#include <cstring>#include <iostream>using namespace std;const int M = 5471; //hash表大小struct node { //节点     char str[31];    int num;    node *next;    node(char *ch, int n) {//构造         strcpy(str, ch);        num = n;    }}; struct hashTable {//hash表     node *link;}hash[M];int maxNum;char ans[31], str[31];void init() {//初始化     for (int i=0; i<M; ++i) hash[i].link = NULL;    return ;}unsigned int BKDRHash(char *str) {//hash    unsigned seed = 131;    unsigned hash = 0;    while (*str) hash = hash *seed + (*str++);    return hash & 0x7FFFFFFF;}void insertAndFind(char *str) {//插入同时查找     int k = BKDRHash(str) % M;    node *p = hash[k].link;    while (p) {        if (!strcmp(p->str, str)) {            ++p->num;            if (p->num >maxNum) {                maxNum = p->num;                strcpy(ans, p->str);                return ;            }        }        p = p->next;    }    node *q = new node(str, 1);    q->next = hash[k].link;    hash[k].link = q;    return ;}void del(node *p) {//释放内存,不然超内存     if (!p) return ;    del(p->next);    delete p;}int main(){    int n;    while (scanf("%d", &n) != EOF) {        init();          maxNum = 1;        for (int i=0; i<n; ++i) {            scanf ("%s", str);            int j;            for (j=0; str[j]=='0'; ++j);//去掉前导0             insertAndFind(str+j);        }        for (int i=0; i<M; ++i) del(hash[i].link);        printf ("%d\n", maxNum);    }    return 0;}