HDOJ1800(Flying to the Mars)

 

#include <iostream>
#include 
<string>
using namespace std;

typedef 
struct
 dicTNode
{
    
struct dicTNode *child[10
];
    
int
 cnt;
}
dicTNode,*dictree;


int maxv;//记录出现次数最多


void init(dictree &root)
{
    root 
= new
 dicTNode;
    
for(int i = 0; i < 10; i++
)
        root
->child[i] =
 NULL;
    root
->cnt = 0
;
    maxv 
= -1
;
}


void insert(string str, dictree &root)
{
    dictree current, newnode;
    
int len =
 str.length();
    
int
 i, j;
    current 
=
 root;
    
for(i = 0; i < len; i++
)
    
{
        
if(current->child[str[i] - '0'
])
        
{
            current 
= current->child[str[i] - '0'
];
        }

        
else
        
{
            newnode 
= new
 dicTNode;
            
for(j = 0; j < 10; j++
)
                newnode
->child[j] =
 NULL;
            newnode
->cnt = 0
;
            current
->child[str[i] - '0'=
 newnode;
            current 
= current->child[str[i] - '0'
];
        }

    }

    current
->cnt++;
    
if(current->cnt >
 maxv)
        maxv 
= current->
cnt;
}


void dfs(dictree &tt)
{//销毁字典树
    if(tt)
    
{
        
for(int i = 0; i < 10; i++
)
        dfs(tt
->
child[i]);
    }

    free(tt);
    tt 
= NULL;

}


int main()
{
    
int
 n, j, ll;
    
string
 str,tmp;
    dictree root;
    
while(cin>>
n)
    
{
        init(root);
        
while(n--
)
        
{

            cin
>>
tmp;
            j 
= 0;ll =
 tmp.length();
            
while(tmp[j] == '0')//去前缀0

                j++;
            ll 
-=
 j;
            str 
=
 tmp.substr(j,ll);
            insert(str, root);
        }

        cout
<<maxv<<endl;
        dfs(root);
    }

    
return 0;
}
 

 

posted on 2009-04-09 12:27  Xredman  阅读(268)  评论(0编辑  收藏  举报

导航