My fault: Originally instead of%f output, I used the%LF, the result of the compiler directly to determine the error (part of the compiler that the LF is not wrong). I thought it was a hash error.
There are more than one method:
Method Time Space
Hash 891ms 596k
Map<string,int> 2735ms 1316k
Sort 5000ms+ 30000k+
The specific differences between%LF and%f:
printf's%f specifier does not only output float type but also output double type. The float type is promoted to type double, according to the default parameter promotion rule. So printf () will only see double-precision numbers. For scanf, the situation is completely different, it accepts pointers, there is no similar type promotion here. Storage to float is not as large as double storage, so scanf distinguishes between%f and%LF.
That is, the output of the time regardless of whether the output is double or single precision with%f is correct, but the input, the input single precision to use%f and input double precision to use%LF.
Hash
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<sstream>using namespacestd;structtree{Charname[ -]; intsum;} tree[10005];intSdbmhash (Char*str) { intHash =0; while(*str) {Hash= (*str++) + (Hash <<6) + (Hash << -) -Hash; } return(hash&0x7FFFFFFF);}BOOLCMP (Tree a,tree b) {returnstrcmp (A.name,b.name) <0;} Map<int,int>Pos;intMain () {//freopen ("H.in.cpp", "R", stdin); Chartmp[ -]; Pos.clear (); inttot =0, all =0; while(Gets (tmp)) {if(STRCMP (TMP,"") ==0) Break; All++; intH =Sdbmhash (TMP); intID =Pos[h]; if(id = =0) {strcpy (tree[++tot].name,tmp); Tree[tot].sum=1; POS[H]=tot; } Else{tree[id].sum++; }} sort (tree+1, tree+1+tot,cmp); for(inti =1; I <= tot; i++) {printf ("%s%.4f\n", tree[i].name,tree[i].sum*100.0/All ); } return 0;}
View Code
Map<string,int>
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<sstream>using namespacestd;structtree{stringname; intsum;} tree[10005];BOOLCMP (Tree a,tree b) {returnA.name <B.name;} Map<string,int>Vis;intMain () {//freopen ("H.in.cpp", "R", stdin); Chara[ -]; stringtmp; Vis.clear (); inttot =0, all =0; while(Gets (a)) { All++; intLen =strlen (a); TMP=""; for(inti =0; i < Len; i++) {tmp+=A[i]; } intID =Vis[tmp]; if(id = =0) {tree[++tot].name =tmp; Tree[tot].sum=1; VIS[TMP]=tot; } Else{tree[id].sum++; }} sort (tree+1, tree+tot+1, CMP); for(inti =1; I <= tot; i++) {cout<<tree[i].name<<" "; printf ("%.4f\n", tree[i].sum*100.0/All ); } return 0;}
View Code
Sort
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<map>#include<sstream>using namespacestd;structtree{Charname[ -];} tree[1000005];BOOLCMP (Tree a,tree b) {returnstrcmp (A.name,b.name) <0;}intMain () {//freopen ("H.in.cpp", "R", stdin); intall =0; while(Gets (tree[all].name) &&strlen (Tree[all].name)) { All++; } sort (Tree,tree+all,cmp); inttot =1; for(inti =0; i < All; i++) { if(strcmp (tree[i].name,tree[i+1].name) = =0) tot++; Else{printf ("%s%.4f\n", tree[i].name,tot*100.0/All ); Tot=1; } } return 0;}
View Code
POJ 2418 Hardwood species (hash,%f and%LF)