Data structure: Inverted index

Source: Internet
Author: User

#include <iostream>#include <map>#include <vector>#include <string.h>using namespace STD;//Inverted index, with attribute values to store the keyword. //Find two people with the highest similarity, similarity = (number of same hobbies)/total number of hobbies. structnode{stringLikeintCount Node () {count =0; } Map<string,bool>MP;};Template<intN>classmyclass{ Public: MyClass () {node =NewNode[n]; index =0; } ~myclass () {if(node)Delete[] node; }voidInsert (Char(*STR) [Ten],intN) {intj =0;string_name = str[j++]; for(; J < N; j + +) {inti =0;string_like = Str[j]; while(Node[i].count! =0&& (node[i].like! = _like)) i++;if(Node[i].count = =0) {Node[i].mp.insert (pair<string,BOOL> (_name,true));                Node[i].like = _like;            node[i].count++; }Else{node[i].count++; Node[i].mp.insert (pair<string,BOOL> (_name,true)); }        }    }voidPrintf () {inti =0; for(; node[i].count!=0; i++) {cout<< node[i].like.c_str () <<" : "; Map<string,bool>:: Iterator it = Node[i].mp.begin (); while(It! = Node[i].mp.end ()) {cout<< (*it). FIRST.C_STR () <<"  ";            it++; }cout<< Endl; }    }Private:intIndex Node *node;};intMain () {myclass<Ten> mc;Charpeople1[][Ten] = {"Small A","selfie","Sports","Game"};Charpeople2[][Ten] = {"Little B","Game","Listen to the song","See the movie"};Charpeople3[][Ten] = {"Little C","Shopping","Listen to the song","Sports"};Charpeople4[][Ten] = {"Little D","Game","Listen to the song","See the movie","Mountain climbing"}; Mc. Insert (People1,4); Mc. Insert (People2,4); Mc. Insert (People3,4); Mc. Insert (People4,5); Mc. Printf ();return 0;}

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Data structure: Inverted index

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: 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.