Poj 1840 hash link address to avoid conflict

Source: Internet
Author: User

It's almost the first time to do hash.AlgorithmToday, we use the map in STL and the link address written by ourselves to avoid conflicting hash arrays as poj 1840, surprisingly, the efficiency of the two methods is actually less than 1300 ms (lT = 5000 ms). I don't know whether the efficiency of my own hash function is good or the STL is too slow.

The followingCodeIs a self-written hash function:

# Include <iostream> <br/> using namespace STD; <br/> struct node <br/> {<br/> long num; <br/> int count; <br/> node * Next; <br/>}; <br/> const int P = 19997; <br/> node * hash [20000]; <br/> int ans = 0; <br/> void insert (long Val) <br/>{< br/> int T; <br/> If (Val> 0) <br/> T = Val % P; <br/> else <br/> T = (0-val) % P; <br/> node * n = hash [T]; <br/> while (n! = NULL) <br/>{< br/> If (n-> num = Val) <br/>{< br/> N-> count ++; <br/> break; <br/>}< br/> N = N-> next; <br/>}< br/> If (n = NULL) <br/>{< br/> N = new node; <br/> N-> COUNT = 1; <br/> N-> num = val; <br/> N-> next = hash [T]; <br/> hash [T] = N; <br/>}< br/> int find (long Val) <br/>{< br/> int T; <br/> If (Val> 0) <br/> T = Val % P; <br/> else <br/> T = (0-val) % P; <br/> node * n = hash [T]; <br/> while (n! = NULL) <br/>{< br/> If (n-> num = 0-val) <br/> return N-> count; <br/> N = N-> next; <br/>}< br/> return 0; <br/>}< br/> void left (int, int B) <br/>{< br/> long Val; <br/> int I = 0; <br/> Int J = 0; <br/> for (I =-50; I <= 50; I ++) <br/> {<br/> if (I = 0) <br/> continue; <br/> for (j =-50; j <= 50; j ++) <br/>{< br/> If (j = 0) <br/> continue; <br/> val = A * I * I + B * j; <br/> insert (VAL ); <br/>}< br/> void right (int c, int D, int e) <br/>{< br/> long Val; <br/> int I = 0; <br/> Int J = 0; <br/> int K = 0; <br/> for (I =-50; I <= 50; I ++) <br/> {<br/> if (I = 0) <br/> continue; <br/> for (j =-50; j <= 50; j ++) <br/>{< br/> If (j = 0) <br/> continue; <br/> for (k =-50; k <= 50; k ++) <br/>{< br/> If (k = 0) <br/> continue; <br/> val = C * I * I + D * j + E * K; <br/> ans + = find (VAL ); <br/>}< br/> void solve (int A, int B, int C, int D, int e) <br/>{< br/> left (a, B); <br/> right (c, d, e ); <br/>}< br/> int main () <br/>{< br/> int A, B, C, D, E; <br/> int I = 0; <br/> CIN> A; <br/> CIN> B; <br/> CIN> C; <br/> CIN> D; <br/> CIN> E; <br/> for (I = 0; I <20000; I ++) <br/>{< br/> hash [I] = NULL; <br/>}< br/> solve (A, B, C, D, E ); <br/> cout <ans <Endl; <br/>}

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.