《 字典树模板_递归 》

 1 #include <iostream>
 2 #include <cstdlib>
 3 #include <cstdio>
 4
 5
 6 using namespace std;
 7
 8 struct tree
 9 {
10     int val;
11     tree *next[26];
12 };
13 tree *head;
14
15 void Insert(tree *p, char *ch, int pp)
16 {
17     if (ch[pp] == ‘\0‘)
18     {
19         p->val++;
20         return;
21     }
22     if (p->next[ch[pp] - ‘a‘] == NULL)
23     {
24         tree *q = (tree *)malloc(sizeof(tree));
25         q->val = 0;
26         for (int i = 0; i < 26; i++)
27         {
28
29             q->next[i] = NULL;
30         }
31         p->next[ch[pp] - ‘a‘] = q;
32
33     }
34     Insert(p->next[ch[pp] - ‘a‘], ch, pp+1);
35 }
36
37 int Query(tree *p, char *ch, int pp)
38 {
39     if(ch[pp] == ‘\0‘)
40         return p->val;
41     if(p->next[ch[pp] - ‘a‘] == NULL)
42         return 0;
43     else
44         Query(p->next[ch[pp] - ‘a‘], ch, pp+1);
45 }
46
47 int Query(char *ch)
48 {
49     return Query(head, ch, 0);
50 }
51
52
53 int main()
54 {
55
56     char a[250], b[250];
57     while(scanf("%s%s",a,b) != EOF)
58     {
59         head = (tree *)malloc(sizeof(tree));
60         head->val = 0;
61         for(int i = 0; i < 26; i++)
62         {
63             head->next[i] = NULL;
64         }
65         Insert(head,a,0);
66         cout<<Query(b)<<endl;
67     }
68
69 }
时间: 2024-08-13 13:31:31

《 字典树模板_递归 》的相关文章

字典树模板题 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统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一