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)