hdu1800 (hash or map)

Source: Internet
Author: User

Test instructions is probably the largest number of repeated occurrences of the string, with a map can be water, but the time is slow, the RP is not good will tle, see Server Luo, just learned the hash, with a hash is never timed out

Write a map of the writing, 998ms,760,670 also tle once, is drunk

#include <iostream> #include <cstring> #include <queue> #include <cstdio> #include < algorithm> #include <string> #include <map>using namespace Std;int main () {#ifdef XXZ    freopen (" In.txt "," R ", stdin); #endif    int n;    while (Cin>>n)    {        map<string,int> MP;        for (int i = 0; i < n; i++)        {            string A;            cin>>a;            for (String::iterator it = A.begin (); *its = = ' 0 '; a.erase (ITS);//To remove the leading 0, otherwise the resulting hash value is different            mp[a]++;        }        Map<string,int>::iterator it;        int sum = 0;        for (it = Mp.begin (); It! = Mp.end (); it++)        {            sum = max (Sum,it->second);        }        cout<<sum<<endl;    }    return 0;}


And then hash,202ms out.

#include <iostream> #include <limits.h> #include <string.h> #include <string> #include < Algorithm> #include <stdio.h>using namespace std;int n;int hash[3010];char str[40];int bkdrhash (char* s) {long L    Ong seed=131;    Long Long hash=0;    while (*s== ' 0 ') s++;//This is important because 0000345645 and 34564 are a number, but if not processed it will correspond to a different hash value while (*s) {hash=hash*seed+ (*s++); } return (hash & 0x7FFFFFFF);}    int main () {int i,ans;        while (scanf ("%d", &n)!=eof) {I=0,ans=1;            for (i=0;i<n;i++) {scanf ("%s", str);        Hash[i]=bkdrhash (str);        } sort (hash,hash+n);        int temp=1;                for (i=1;i<n;i++)//ans is the maximum value of the equal number {if (hash[i]==hash[i-1]) {temp++;            if (Temp>ans) ans=temp;        } else temp=1;    } printf ("%d\n", ans); } return 0;}



hdu1800 (hash or map)

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.