It's boring to get a dictionary tree template. I have never understood dictionary trees very well .. You may want to learn more about the data structure .. This is important ..
Prefix:
# Include <iostream>
# Include <string>
# Include <algorithm>
Using namespace STD;
# Define Maxs 26
# Define maxl 22
# Define maxn1005
Char STR [maxn] [maxl];
Struct Node
{
Int num; // num indicates the number of strings that use the node. If num is 1, the node is the only node of the string.
Node * Next [Maxs];
Node ()
{
Num = 0;
For (INT I = 0; I <Maxs; I ++)
{
Next [I] = NULL;
}
}
} * Root;
Void insert (node * RT, char * Str)
{
Int I, ID, Len;
Len = strlen (STR );
For (I = 0; I <Len; I ++)
{
Id = STR [I]-'A ';
If (RT-> next [ID] = NULL)
RT-> next [ID] = new node ();
RT = RT-> next [ID];
RT-> num ++;
}
}
Void serch (node * RT, char * s)
{
Int I, ID, Len;
Len = strlen (s );
For (I = 0; I <Len; I ++)
{
Id = s [I]-'A ';
If (RT-> num = 1) // The End of the search. The shortest prefix has been found.
Break;
Printf ("% C", s [I]);
RT = RT-> next [ID];
}
Printf ("\ n ");
}
Int main ()
{
Int Total = 0;
Int I;
Root = new node ();
While (scanf ("% s", & STR [total])! = EOF)
{
Insert (root, STR [total]);
Total ++;
}
For (I = 0; I <total; I ++)
{
Printf ("% s", STR [I]);
Serch (root, STR [I]);
}
Return 0;
}
Corresponding to the following suffixes:
# Include <iostream>
Using namespace STD;
Const int maxm = 30, kind = 26;
Int m;
Struct Node
{
Char * s;
Int prefix;
Bool isword;
Node * Next [kind];
Node ()
{
S = NULL;
Prefix = 0;
Isword = false;
Memset (next, 0, sizeof (next ));
}
} * Root; // Root
Void insert (node * root, char * s) // insert
{
Node * P = root;
For (INT I = 0; s [I]; I ++)
{
Int x = s [I]-'A ';
P-> S = S + I;
If (p-> next [x] = NULL)
P-> next [x] = new node;
P = p-> next [x];
P-> prefix ++;
}
P-> isword = true;
}
Bool del (node * root, char * s) // Delete
{
Node * P = root;
For (INT I = 0; s [I]; I ++)
{
Int x = s [I]-'A ';
If (p-> next [x] = NULL)
Return false;
P = p-> next [x];
}
If (p-> isword)
P-> isword = false;
Else
Return false;
Return true;
}
Bool search (node * root, char * s) // search
{
Node * P = root;
For (INT I = 0; s [I]; I ++)
{
Int x = s [I]-'A ';
If (p-> next [x] = NULL)
Return false;
P = p-> next [x];
}
Return p-> isword;
}
Int count (node * root, char * s) // statistics suffix
{
Node * P = root;
For (INT I = 0; s [I]; I ++)
{
Int x = s [I]-'A ';
If (p-> next [x] = NULL)
Return 0;
P = p-> next [x];
}
Return p-> prefix;
}
Int main ()
{
M = 0;
Root = new node;
Char s [maxm];
While (gets (s ))
{
If (strcmp (S, "") = 0)
Break;
Insert (root, S );
}
While (gets (s ))
Printf ("% d \ n", count (root, s ));
Return 0;
}