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

字典树就是将一个个单词按照字母顺序建成树,可以用于单词去重、计算每种单词的出现次数、计算共出现多少种单词

 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型仅用于判断是否为单词结束并可以查询,将其换成int型也能统计每个单词出现的次数
 8     int size;
 9
10     void init(){                    //初始化函数
11         memset(nxt,-1,sizeof(nxt));
12         memset(tail,0,sizeof(tail));
13         size=0;
14     }
15
16     void insert(char word[]){    //添加word
17         int root=0,l=strlen(word);
18         for(int i=0;i<l;i++){
19             if(nxt[root][word[i]-‘a‘]<0){
20                 nxt[root][word[i]-‘a‘]=++size;
21             }
22             root=nxt[root][word[i]-‘a‘];
23         }
24         if(!tail[root]&&root!=0)tail[root]=1;        //此处可以加计数器统计共出现多少种单词
25     }
26
27     bool find(char word[]){                //查询word,若tail数组为int型,此处返回值也为int型可用于查询单词出现次数
28         int root=0,l=strlen(word);
29         for(int i=0;i<l;i++){
30             if(nxt[root][word[i]-‘a‘]<0)return 0;
31             root=nxt[root][word[i]-‘a‘];
32         }
33         return tail[root];
34     }
35 };
时间: 2024-08-02 07:02:03

字符串匹配--字典树模板的相关文章

字典树模板题 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 1251 统计难题(字典树模板题 || map运用)

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

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

静态字典树模板

#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 统计难题(字典树模板题)

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