#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