Question on Pairing users by Attributes.. Algorithm/Data Structure
I have a question on algorithm that I am stuck in.
We are given a sequence of users, and set of attributes for each user. As soon as we read a user, we should pair it with another previously read user with identical attributes who is currently unpaired, if such a user exists.
If the user cannot be paired, we should keep that user in the unpaired set.
The QUESTIONS are,
- How would we implement this matching process efficiently...
- How would we implement it if we allow an approximate match of attributes as well...
The solution to first question, I found is using trie data-structure, wherein we arrange the attributes of each 开发者_StackOverflow社区user in sorted order and start inserting the atttributes into the trie, with user attached to the leaf. If a new user comes in, we follow the same trie and if we found previous user attached to the leaf, then we found the pair or else we insert that new user into the leaf.
Kindly suggest your idea or any new thought on this solution and kindly help me in solving the second question. I am completely clueless in 2nd question..
Explanation with some sample codes or an example would be helpful..
Thanks...
Hash table of all unpaired users, key = user attribute.
When use enters, and is assigned the attribute, lookup him in the hash.
If found, then he is paired. Remove the 2nd (paired user) use from hash.
Do not add just entered user to th hash.
If not found, add him to the hash (he is awating his pair).
Done
精彩评论