Look for hash values--think of an int array as an integer

Source: Internet
Author: User

QUESTION:

Write A class dominochecker that have a method called Addbox (int[]) that takes a box of five dominoes, described as a list of integers (explained after), adds it-a collection, and returns True if a box with the same dominoes is already in The collection and False otherwise. A box of dominoes is encoded as a list of ten integers from 0 to 9, where a pair of numbers represent a domino. For example:0,2,9,1,3,3,7,4,5,6 represents a box containing dominoes: (0,2); (9,1); (3,3); (7,4); (5,6). Solution:
typedef pair<int,int>Domino;classDominochecker {unordered_set<Long Long>Hash; Vector< vector< Domino > >boxes;  Public:    BOOLAddbox (Constvector<int>&box) {Vector< Domino >v;  for(inti =0; I <5; i++) {Domino D (box[2*i], box[2*i+1]); if(D.first >d.second) Swap (D.first, d.second); //order does not matterV.push_back (d);  } sort (V.begin (), V.end ()); //order does not matter.        Long LongHash_value =0;  for(inti =0; I <5; i++) Hash_value= Hash_value * -+ v[i].first*Ten+ V[i].second;//consider an int array as an integer        if(Hash.find (hash_value)! =hash.end ())return false;        Hash.insert (Hash_value); Boxes.push_back (v); //I suppose we want to store them        return true; }};

Look for hash values--think of an int array as an integer

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.