Fuzzy Google Suggest [未完成]

本文介绍了如何使用 Trie 结构实现智能模糊搜索,并详细解释了搜索算法的实现过程及其实现细节。
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <complex>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <cassert>
#define f(x) (x - 'a')
#define BUG cout << "here\n";
using namespace std;
const int kind = 26;
struct Node { /// Trie 模糊搜索 [解决智能模糊搜索问题]
    char value;
    Node* pre;
    Node* next[kind];
    int num;
};
int n, m;
void calc(Node* p, char *s, int d, set<Node*>& gsv) {
    if(p) {
        cout << p->value << "啦啦"<< endl;
        system("pause");
        if(*s) {
            calc(p->next[f(*s)], s+1, d, gsv);

            if(d) {
                for(int i = 0; i < 26; i++) {
                    calc(p->next[i], s, d-1, gsv);  /// 库里面的串 删除 一个字符
                }
                calc(p, s+1, d-1, gsv);   /// 插入 [相当于库里面的串插入一个字符]
                for(int i = 0; i < 26; i++) {
                    //BUG
                    calc(p->next[i], s+1, d-1, gsv); /// 替换
                }
                cout << "呵呵\n";
            }

        }
        else {
            cout << p << ' ';
            cout << p->value << endl;
            gsv.insert(p);
        }
    }
}
int main() {
    Node* root = new Node();
    cin >> n;
    for(int i = 0; i < n; i++) {
        char ch[20];
        cin >> ch;
        Node* p = root;
        char* s = ch;
        while(*s) {
            if(!p->next[f(*s)]) {
                p->next[f(*s)] = new Node();
                p->next[f(*s)]->pre = p;
            }
            p = p->next[f(*s)];
            p->value = *s;
            p->num++;;
            s++;
        }
    }
    cin >> m;
    for(int i = 0; i < m; i++) {
        char s[20];
        int d;
        cin >> s >> d;
        set<Node*> gsv;
        calc(root, s, d, gsv);
        int ans = 0;
        /***/
        for(set<Node*>::iterator it = gsv.begin(); it != gsv.end(); it++) {
            Node* pt = *it;
        }
        /**/
        for(set<Node*>::reverse_iterator it = gsv.rbegin(); it != gsv.rend(); it++) {
            Node* p = *it;
            int count = p->num;
            while(p != root) {
                p = p->pre;
                set<Node*>::iterator t = gsv.find(p);
                if(t != gsv.end()) count = 0; /// 找到就为 0
            }
           ans += count;
        }
        cout << ans << endl;
    }
    return 0;
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值