字典树模板( 指针版 && 数组版 )

模板 :

#include<string.h>
#include<stdio.h>
#include<malloc.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 26;

struct Trie
{
    Trie *Next[maxn];
    int v;
    inline void init(){
        this->v = 1;
        for(int i=0; i<maxn; i++)
            this->Next[i] = NULL;
    }
};
Trie *root = (Trie *)malloc(sizeof(Trie));

void CreateTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root, *tmp;
    for(int i=0; i<len; i++){
        int idx = str[i]-‘a‘;

        if(p->Next[idx] == NULL){
            tmp = (Trie *)malloc(sizeof(Trie));
            tmp->init();
            p->Next[idx] = tmp;
        }else p->Next[idx]->v++;

        p = p->Next[idx];
    }
    p->v = -1;//若为结尾,则将v改成-1,当然字典树里面的变量都是要依据题目
              //设置并且在特定位置赋值的
}

int FindTrie(char *str)
{
    int len = strlen(str);
    Trie *p = root;
    for(int i=0; i<len; i++){
        int idx = str[i]-‘a‘;
        p = p->Next[idx];
        //...进行一系列操作
    }
}

inline void DelTrie(Trie *T)
{
    if(T == NULL) return ;
    for(int i=0; i<maxn; i++){
        if(T->Next[i] != NULL)
            DelTrie(T->Next[i]);
    }
    free(T);
    return ;
}

int main(void)
{
    root.init();//!!!
    //...
}

指针版

数组版

字典树算法参考 ==> http://www.cnblogs.com/tanky_woo/archive/2010/09/24/1833717.html

时间: 2024-08-28 16:32:57

字典树模板( 指针版 && 数组版 )的相关文章

字典树模板 [HDU 1251] 统计难题

统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submission(s): 19054    Accepted Submission(s): 8418 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前

【统计难题】【HDU - 1251】【map打表或字典树】【字典树模板】

思路 题意:题目为中文题,这里不再过多阐述. 思路1:可以在读入单词表的过程中将单词分解,用map将它一 一记录 思路2:利用字典树,这个方法较快些,下面代码中会分别给出数组和结构体指针两种形式的字典树,指针形式的有时可能会因题目内存限制而导致Memory Limit Exceeded,这时就可选择数组形式的.不过对于本题来说不用担心. AC代码 代码1:map打表 #include<bits/stdc++.h> using namespace std; typedef long long L

字典树模板题 POJ 2503

1 #include <cstdio> 2 #include <cstring> 3 4 char en[11],fr[11]; 5 int st; 6 struct Tire{ 7 int next[26]; 8 char eng[11]; 9 }node[200005]; 10 void insert(char *s,int cur) 11 { 12 if(*s){ 13 if(!node[cur].next[*s-'a']) 14 node[cur].next[*s-'a']

静态字典树模板

#include <iostream> #include <cstring> #include <cstdio> using namespace std; int pos; struct node { int child[26]; }tree[10000010]; int add() { pos++; for(int i=0;i<26;i++) { tree[pos].child[i]=-1; } return pos; } int inser(char* str

Vasiliy&#39;s Multiset CodeForces -706D || 01字典树模板

就是一个模板 注意这题有一个要求:有一个额外的0一直保持在集合中 1 #include<cstdio> 2 #include<algorithm> 3 using namespace std; 4 const int _LEN=30; 5 //上限为2^30-1,即二进制最多30位 6 int lft[40]; 7 namespace Trie 8 { 9 int ch[7001000][2],sz[7001000]; 10 int mem; 11 void insert(int

字典树模板+HDU 1671 ( Phone List )(字典树)

字典树指针模板(数组模板暂时还没写): 1 #include<cstdio> 2 #include<string.h> 3 #include<algorithm> 4 using namespace std; 5 const int MAX=26; 6 const int maxn=1e4+100; 7 int N; 8 9 struct Trie 10 { 11 Trie *next[MAX]; 12 int v;///v要灵活使用,具体情况具体分析 13 }; 14

字符串匹配--字典树模板

字典树就是将一个个单词按照字母顺序建成树,可以用于单词去重.计算每种单词的出现次数.计算共出现多少种单词 1 #include<stdio.h> 2 #include<string.h> 3 const int maxm=5050; //所有单词的总长度,约总单词数*5 4 5 struct trie{ 6 int nxt[maxm][26]; 7 bool tail[maxm]; //记录某个结点是否为单词结束,用于统计或标记单词,bool型仅用于判断是否为单词结束并可以查询,将

(模板)hdoj1251(字典树模板题)

题目链接:https://vjudge.net/problem/HDU-1251 题意:给定一系列字符串之后,再给定一系列前缀,对每个前缀查询以该字符串为前缀的字符串个数. 思路: 今天开始学字典树,从入门题开始.用数组实现,count数组表示每个结点出现次数,trie[0]为根节点.插入和查询一个字符串的复杂度为O(len).len为字符串的长度. AC code: #include<cstdio> #include<algorithm> #include<cstring&

字典树模板(java)

class Trie{ private int SIZE=26; private TrieNode root;//字典树的根 Trie(){//初始化字典树 root=new TrieNode(); } private class TrieNode{//字典树节点 private int num;//有多少单词通过这个节点,即节点字符出现的次数 private TrieNode[] son;//所有的儿子节点 private boolean isEnd;//是不是最后一个节点 private c