HDU 1800--flying to the Mars —————— "string hash"

Source: Internet
Author: User

Flying to the Mars

Time limit:5000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 16049 Accepted Submission (s): 5154


Problem Description
The year 8888, the Earth was 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 has to remind your 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-fly on a broomstick! We assume that one soldier have one level number indicating his degree. The soldier who have a higher level could teach the lower, that's to say the former's level > the latter ' s. But the lower can ' t teach the higher. One soldier can has only one teacher at the most, certainly, has no teacher is also legal. Similarly one soldier can has only one StUdent at the most and have no student is also possible. Teacher can teach his student on the same broomstick. Certainly, all the soldier must has 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 is 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 is eligible to study on the same broomstick.
D could teach E; So D E is eligible to study on the same broomstick;
Using This method, we need 2 broomsticks.
Another method:
D could teach A; So A D is eligible to study on the same broomstick.
C could teach B; So B C is eligible to study on the same broomstick.
E with no teacher or student is eligible to study on one broomstick.
Using the method, we need 3 broomsticks.
......

After checking the-possible method, we found that 2 are the minimum number of broomsticks needed.

Inputinput 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 an integer on each line and indicating the level number for each soldier. (less than digits);

Outputfor each case, output the minimum number of broomsticks on a.

Sample Input410203004 523434

Sample Output12

Author[email protected]

The main topic: give you the number of N, let you judge the number of the most likely to be the number of times. Because the given number has dozens of bits, long long also cannot save.  So we use hash to record each number, and record the number of times. Because the number of n given will have 00001 of this prefix 0 of the data, so we have to deal with it first.

#include <stdio.h> #include <algorithm> #include <string.h> #include <iostream>using namespace    std;const int maxn = 1e4+200;struct chain{char st[200];    int TM;    Chain *next;        Chain () {tm = 0;    Next = NULL;    } ~chain () {}}hash_table[maxn];int ans;//bkdrint Hash (char *s) {unsigned int seed = 131;    13,131,1313 13131 131313 unsigned int v_hash = 0;    while (*s = = ' 0 ') {++s;    } while (*s) {V_hash = V_hash * seed + (*s++); } return (V_hash & 0x7fffffff);}    void Insert (Chain *rt, char *s) {while (*s = = ' 0 ') s++;    printf ("%s\n", s);        while (RT-to-next = NULL) {RT = rt->next;            if (strcmp (rt->st,s) ==0) {rt->tm++;            ans = max (ANS,RT-&GT;TM);        return;    }} rt->next = new Chain ();    RT = rt->next;    Rt->next = NULL;    strcpy (rt->st,s);    rt->tm++; ans = max (ans,1);} void free (Chain * rt) {if (Rt->next! = NULL) FrEE (rt->next);        {Delete rt;    }}int Main () {char S[MAXN];    int n, m;    unsigned int H;        while (scanf ("%d", &n)!=eof) {ans = 0;            for (int i = 1; I <= n; ++i) {scanf ("%s", s);          H = Hash (s)%2911;            printf ("%u+++++++++++\n", H);        Insert (&hash_table[h],s);        } printf ("%d\n", ans);            for (int i = 0; I <= 3200; i++) {if (Hash_table[i].next = = NULL) continue;            Free (hash_table[i].next);        Hash_table[i].next = NULL; }} return 0;} /*50000001000000141000100001011*/

  

HDU 1800--flying to the Mars —————— "string hash"

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.