Poj2549hash Separation Link Method

Source: Internet
Author: User
Tags hash
Test instructions
Give n a different number, ask for a combination of 4 numbers (A,B,C,D) to satisfy the A+b+c=d, and the largest d.
Ideas:
I didn't think I could use a hash.
This is the data structure of the separation link method ~
Resolving hash Conflicts: Links all keywords to synonyms in the same single-linked list.
A+b+c=d converted into a+b=d-c;
Hash out all the a+b first.
Then use D-c to find.

Complexity of N^2*hash;

#include <cstdio> #include <string.h> #include <algorithm> using namespace std;
const int MOD=5E5+10;
const int N=5E5+10;
struct node{int x, y;};
Node Q[n];
int ha[mod],nex[mod],s[1010],res,n;
BOOL Flag;
    void solve (int x,int y) {int key= (x-y+mod)%mod;
    int I=ha[key]; while (I!=-1) {while (q[i].x+q[i].y) = = (x-y) &&q[i].x!=x&&q[i].x!=y&&q[i].y!=y&&
            q[i].y!=x) {res=x;
            Flag=true;
        Return
    } I=nex[i];
        }} int main () {while (scanf ("%d", &n) &&n) {for (int i=0;i<n;i++) scanf ("%d", &s[i]);
        int tot=0;
        Sort (s,s+n);
        memset (ha,-1,sizeof (Ha)); 
                for (int i=0;i<n-1;i++) for (int j=i+1;j<n;j++) {q[tot].x=s[j];q[tot].y=s[i];
                int key= (s[i]+s[j]+mod)%mod;
                Nex[tot]=ha[key];
                Ha[key]=tot;
        tot++;    } res=-1000000000;
        Flag=false;
                for (int i=n-1;i>=1;i--) for (int. j=i-1;j>=0;j--) {if (flag) break;
            Solve (s[i],s[j]);
        } if (res!=-1000000000) printf ("%d\n", res);
    Else puts ("no solution");
} return 0;

 }



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.