Dictionary tree. Each node has 10 leaf nodes. Note that the value 0 must be removed from the prefix.
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
Using namespace std;
Int Max;
Struct node
{
Int count;
Node * childs [10];
Node ()
{
Count = 0;
Int I;
For (I = 0; I <= 9; I ++)
Childs [I] = NULL;
}
};
Node * root, * current, * newnode;
Void insert (char * str)
{
Int I, m;
Current = root;
For (I = 0; I <strlen (str); I ++)
{
M = str [I]-'0 ';
If (current-> childs [m]! = NULL)
Current = current-> childs [m];
Else
{
Newnode = new node;
Current-> childs [m] = newnode;
Current = newnode;
}
}
++ (Current-> count );
If (current-> count)> Max)
Max = (current-> count );
}
Int main ()
{
Int n, I, len;
Char s [40], str [40];
While (scanf ("% d", & n )! = EOF)
{
Max =-1;
Root = new node;
While (n --)
{
Scanf ("% s", s );
Len = strlen (s );
For (I = 0; I <len-1; I ++)
If (s [I]! = '0 ')
Break;
If (I = len-1)
{
Str [0] = s [len-1];
Str [1] = '\ 0 ';
}
Else
{
Strncpy (str, & s [I], len-I );
Str [len-I] = '\ 0 ';
}
Insert (str );
}
Printf ("% d \ n", Max );
}
Return 0;
By Cambridgeacm