字典树模板

    xiaoxiao2025-08-01  6

    #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <queue> #include <math.h> #define MOD 1000000007 using namespace std; const int M = 100000; struct node{ int next[26]; int value; bool end; }; node tree[M]; int pi = 1; void init(){ memset(tree,0,sizeof(tree)); pi = 1; } void insert(char *keyword,int value){ int index,p,i; for(i=p=0;keyword[i]; ++i){ index = keyword[i]-'a'; if(tree[p].next[index]==0){ tree[p].next[index] = pi++; } p = tree[p].next[index]; } tree[p].value = value; tree[p].end = 1; } bool query(char *keyword,int &value){ int index,p,i; for(i=p=0;keyword[i]; ++i){ index = keyword[i]-'a'; if(tree[p].next[index]==0){ return false; } p = tree[p].next[index]; } if(tree[p].end){ value = tree[p].value; return true; } return false; } int main() { return 0; }
    转载请注明原文地址: https://ju.6miu.com/read-1301298.html
    最新回复(0)