字典树【模板】

struct TrieNode
{
    int Count;<span style="white-space:pre">		</span>//前缀单词出现次数
    struct TrieNode* Next[26];<span style="white-space:pre">	</span>//26个字母节点
}Tree,*Trie;

TrieNode *root;
void Create()   //初始化
{
    root = new TrieNode;
    memset(root->Next,NULL,sizeof(root->Next));
    root->Count = 0;
}

void Insert(char *s)     //插入字符串s[],O(M*L)
{
    TrieNode *p, *q;
    p = root;
    while(*s)
    {
        if(p->Next[*s-'a'] == NULL)
        {
            q = new TrieNode;
            memset(q->Next,NULL,sizeof(q->Next));
            q->Count = 1;
            p->Next[*s-'a'] = q;
        }
        else
            p->Next[*s-'a']->Count++;
        p = p->Next[*s-'a'];
        s++;
    }
}

int Find(char *s)   //查找字符串s[] O(N*L)
{
    TrieNode *p, *q;
    p = root;
    while(*s)
    {
        if(p->Next[*s-'a'] == NULL)
            return 0;
        p = p->Next[*s-'a'];
        s++;
    }
    return p->Count;
}
时间: 2024-08-23 10:06:07

字典树【模板】的相关文章

字典树模板题 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']

字典树模板 [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最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前

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

字典树模板(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

hdu 1247 Hat’s Words (字典树模板)

//那个单词是有出现的两个单词构成的 # include <cstdio> # include <cstring> # include <algorithm> # include <iostream> # define MAX 26 using namespace std; typedef struct Trie_Node { bool isWord; struct Trie_Node *next[MAX]; } Trie; char s[50000][50

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

字典树就是将一个个单词按照字母顺序建成树,可以用于单词去重.计算每种单词的出现次数.计算共出现多少种单词 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型仅用于判断是否为单词结束并可以查询,将

静态字典树模板

#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

HDU 1251 统计难题(字典树模板题 || map运用)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1251 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一

HDU 1251 统计难题(字典树模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 using namespace std; 5 const int maxn = 1000005; 6 7 int num = 0; 8 9 struct Tr