hdu 1251简单字典树

#include<stdio.h>
#include<iostream>
#include<string.h>
using namespace std;
struct node {
int num,i;
node *a[27];
node (){
num=0;
for(i=0;i<27;i++)
a[i]=NULL;
}
}*root;
char str[20];
void insert(int len,node *root) {
int i;
node *q;
for(i=0;i<len;i++) {
if(root->a[str[i]-'a']==NULL) {
q=new node();
q->num++;
root->a[str[i]-'a']=q;
root=q;
}
else  {
root=root->a[str[i]-'a'];
root->num++;
}
}
return ;
}
int find(int len,node *root) {
int i;
for(i=0;i<len;i++) {
root=root->a[str[i]-'a'];
if(root==NULL)
return 0;
}
return root->num;
}
int main() {
int k;
root=new node();
while(gets(str)&&str[0]!='\0') //输入条件太恶心了
insert(strlen(str),root);
while(scanf("%s",str)!=EOF) {
k=find(strlen(str),root);
printf("%d\n",k);
}
return 0;
}



posted @ 2013-11-17 16:41  HYDhyd  阅读(94)  评论(0编辑  收藏  举报