数据结构:倒排索引

#include <iostream>
#include <map>
#include <vector>
#include <string.h>
using namespace std;
//倒排索引,以属性值来存储关键字。

//找出两个人相似度最高的人,相似度=(相同爱好数)/总的爱好数。
struct Node
{
    string like;
    int count;
    Node()
    {
        count = 0;
    }
    map<string,bool> mp;
};

template<int N>
class MyClass
{
public:
    MyClass()
    {
        node = new Node[N];
        index = 0;
    }
    ~MyClass()
    {
        if (node)
        delete[] node;
    }
    void Insert(char (*str)[10],int n)
    {


        int j = 0;
        string _name = str[j++]; 
        for (; j < n; j++)
        {
            int i = 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));
            }
        }

    }
    void Printf()
    {
        int i = 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:
    int index;
    Node *node;
};
int main()
{
    MyClass<10> mc;
    char People1[][10] = { "小A", "自拍", "运动", "游戏" };
    char People2[][10] = { "小B", "游戏", "听歌", "看电影" };
    char People3[][10] = { "小C", "逛街", "听歌", "运动"};
    char People4[][10] = {"小D","游戏","听歌","看电影","爬山"};

    mc.Insert(People1,4);
    mc.Insert(People2,4);
    mc.Insert(People3,4);
    mc.Insert(People4,5);
    mc.Printf();

    return 0;
}

这里写图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值