The principle is very simple,,,, must be able to understand,,, I feel that the realization of the cost of a bit harder .....
My templates:
#include <iostream>
#include <bits/stdc++.h>
using namespace Std;
#define MAX 26
typedef struct TRIENODE
{
int ncount; The number of times the node prefix appears
struct Trienode *next[max]; Subsequent nodes of the node
} Trienode;
Trienode memory[1000000]; Allocate good memory first. malloc is more time consuming
int ALLOCP = 0;
Initializes a node. Ncount count is 1, next is null
Trienode * Createtrienode ()
{
Trienode * tmp = &Memory[allocp++];
Tmp->ncount = 1;
for (int i = 0; i < MAX; i++)
Tmp->next[i] = NULL;
return TMP;
}
void Inserttrie (Trienode * * proot, char * str)
{
Trienode * tmp = *proot;
int i = 0, k;
One-by-one insertion character
while (Str[i])
{
K = Str[i]-' a '; Where the current character should be inserted
if (Tmp->next[k])
{
tmp->next[k]->ncount++;
}
Else
{
Tmp->next[k] = Createtrienode ();
}
TMP = tmp->next[k];
i++; Move to next character
}
}
int Searchtrie (Trienode * root, char * str)
{
if (root = = NULL)
return 0;
Trienode * tmp = root;
int i = 0, k;
while (Str[i])
{
K = Str[i]-' a ';
if (Tmp->next[k])
{
TMP = tmp->next[k];
}
Else
return 0;
i++;
}
Return tmp->ncount; Returns the ncount of the node where the last character is located
}
int main (void)
{
Char s[30];
Trienode *root = Createtrienode ();
while (gets (s) && s[0]! = ' 0 ')//read in 0 end
{
Inserttrie (&root, s);
}
while (gets (s))//Query input string
{
printf ("%d\n", Searchtrie (Root, s));
}
return 0;
}
This method is very quick .....
/* Some problems to use C + + do not g++ or easily super memory * *
HDU1004:
Slightly deformed ...
#include <stdio.h>
#include <string.h>
using namespace Std;
typedef struct NODE
{
struct node *next[26];
int cnt;
}node;
Node memory[100000];
int ALLOCP;
int ans;
Char an[30];
Node *create ()
{
Node *tmp=&memory[allocp++];
for (int i=0;i<26;i++) tmp->next[i]=null;
tmp->cnt=0;
return TMP;
}
int Inserttrie (node **proot,char *s)
{
Node *tmp=*proot;
int i=0,k;
while (S[i])
{
k=s[i]-' a ';
if (Tmp->next[k]);
Else
{
Tmp->next[k]=create ();
}
if (I==strlen (s)-1)
{
tmp->next[k]->cnt++;
printf ("%d*\n", tmp->next[k]->cnt);
}
if (Tmp->next[k]->cnt>=ans)
{
ans=tmp->next[k]->cnt;
strcpy (an,s);
Puts (an);
printf ("%d\n", tmp->next[k]->cnt);
}
tmp=tmp->next[k];
i++;
}
return ans;
}
int main ()
{
int m;
Char a[100];
Node *root=create ();
while (scanf ("%d", &m) &&m!=0)
{
memset (memory,0,sizeof (memory));
Allocp=0;
ans=0;
int i;
for (i=0;i<m;i++)
{
scanf ("%s", a);
Inserttrie (&root,a);
}
printf ("%s\n", an);
}
return 0;
}
HDU1075 can do without a dictionary tree,,,, with a pair on it,, simulation ....
The dictionary tree note such as input FIW,, output or FIW;
#include <stdio.h>
#include <string.h>
using namespace Std;
Char a[15],b[15];
typedef struct NODE
{
Char s[15];
struct node *next[26];
};
Node memory[1000000];
int allocp=0;
Node * Create ()
{
Node * tmp=&memory[allocp++];
for (int i=0;i<26;i++) tmp->next[i]=null;
tmp->s[0]= ' + ';
return TMP;
}
Void Inserttrie (node * * Proot,char *b)
{
node * tmp=*proot;
int i=0,k;
while (B[i])
{
k=b[i]-' A ';
if (tmp->next[k]);
Else
tmp->next[k]=create ();
if (I==strlen (b)-1) {strcpy (tmp->next[k]->s,a);}//printf ("%s \ n ", tmp->next[k]->s);
tmp=tmp->next[k];
i++;
}
}
int Searchtrie (node *root,char *s)
{
if (Root==null) return 0;
node* tmp = root;
int i=0,k;
while (S[i])
{
k=s[i]-' a ';
if (Tmp->next[k])
{
tmp=tmp->next[k];
}
else return 0;
i++;
}
if (tmp->s[0]==0) return 0;
printf ("%s", tmp->s);
return 1;
}
int main ()
{
scanf ("%s", a);
Char c[10001];
Char d[500];
Node * root=create ();
while (scanf ("%s", a) ==1)
{
if (strcmp (A, "END") ==0) break;
scanf ("%s", b);
int flag=0;
Inserttrie (&ROOT,B);
}
scanf ("%s", a);
GetChar ();
while (1)
{
Gets (c);
printf ("%s**\n", c);
if (c[0]== ' S ' &&c[1]== ' t ' &&c[2]== ' A ' &&c[3]== ' R ' &&c[4]== ' t ') continue;
if (c[0]== ' E ' &&c[1]== ' N ' &&c[2]== ' D ') break;
int t=0;
for (int i=0;i<strlen (c); i++)
{
if (c[i]>= ' a ' &&c[i]<= ' Z ')
{
D[t++]=c[i];
if (I==strlen (c)-1) {if (Searchtrie (root,d) ==0) printf ("%s", d);}
}
else if ((c[i-1]>= ' a ' &&c[i-1]<= ' Z '))
{
printf ("%s***\n", D);
if (Searchtrie (root,d) ==0) printf ("%s", d);
printf ("%c", C[i]);
Memset (d,0,sizeof (d));
t=0;
}
else printf ("%c", C[i]);
}
Puts ("");
}
return 0;
}
HDU4287
#include <stdio.h>
#include <string.h>
using namespace Std;
Char a[5005][10];
Char b[10];
typedef struct NODE
{
struct node *next[10];
int cnt;
}node;
Node memory[200000];
int ALLOCP;
void Bianhuan ()
{
for (int i=0;i<strlen (b); i++)
{
if (b[i]== ' A ' | | b[i]== ' B ' | | b[i]== ' C ') b[i]= ' 2 ';
if (b[i]== ' d ' | | b[i]== ' E ' | | b[i]== ' F ') b[i]= ' 3 ';
if (b[i]== ' G ' | | b[i]== ' h ' | | b[i]== ' i ') b[i]= ' 4 ';
if (b[i]== ' J ' | | b[i]== ' k ' | | b[i]== ' l ') b[i]= ' 5 ';
if (b[i]== ' m ' | | b[i]== ' n ' | | b[i]== ' o ') b[i]= ' 6 ';
if (b[i]== ' P ' | | b[i]== ' Q ' | | b[i]== ' r ' | | b[i]== ' s ') b[i]= ' 7 ';
if (b[i]== ' t ' | | b[i]== ' U ' | | b[i]== ' V ') b[i]= ' 8 ';
if (b[i]== ' W ' | | b[i]== ' x ' | | b[i]== ' Y ' | | b[i]== ' z ') b[i]= ' 9 ';
}
}
Node *create ()
{
node *tmp=&memory[allocp++];
tmp->cnt = 0;
for (int i=0;i<10;i++)
{
Tmp->next[i] = NULL;
}
return tmp;
}
Void Inserttrie (node **proot,char *s)
{
node *tmp=*proot;
int i=0,k;
while (S[i])
{
k=s[i]-' 0 ';
if (tmp->next[k]);
Else tmp->next[k]=create ();
tmp=tmp->next[k];
i++;
if (I==strlen (s))
{
tmp->cnt++;
}
}
}
int Searchtrie (node *root,char *s)
{
if (Root==null) return 0;
Node *tmp=root;
int i=0,k;
while (S[i])
{
k=s[i]-' 0 ';
if (Tmp->next[k])
tmp=tmp->next[k];
else return 0;
i++;
}
Return tmp->cnt;
}
int main ()
{
int m;
scanf ("%d", &m);
int w1,w2;
while (m--)
{
memset (memory,0,sizeof (memory));
Allocp=0;
Node *root=create ();
scanf ("%d%d", &w1,&w2);
int i;
for (i=0;i<w1;i++)
scanf ("%s", A[i]);
for (i=0;i<w2;i++)
{
scanf ("%s", b);
Bianhuan ();
Inserttrie (&ROOT,B);
}
printf ("*\n");
for (i=0;i<w1;i++)
printf ("%d\n", Searchtrie (Root,a[i]));
}
return 0;
}
The dictionary tree is almost like this ... nn
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
/* Dictionary Tree * * Some simple questions