hdu 1075 字典树

#include<stdio.h>
#include<iostream>
struct node {
int num,i;
node *a[27];
char s[20];//定义局部不是空的
node() {
num=0;
for(i=0;i<26;i++)
a[i]=NULL;
}
}*root;
char s2[20],str[20],str1[3100],s1[20],h[20];
void insert(int len ,node *root) {
int i;
for(i=0;i<len;i++) {
if(root->a[s1[i]-'a']==NULL) 
root->a[s1[i]-'a']=new node();
root=root->a[s1[i]-'a'];
}
root->num=1;
     strcpy(root->s,s2);
return ;
}
void find (int len,node *root) {
int i;
for(i=0;i<len;i++) {
root=root->a[str[i]-'a'];
if(root==NULL)
            return ;
}
if(root->num==1)//必须判断是否存在
strcpy(h,root->s);
return;
}
void del(node *root) {
int i;
for(i=0;i<26;i++)
if(root->a[i]!=NULL)
del(root->a[i]);
delete root;
}
int main() {
int i,k;
root=new node();
        scanf("%s",str);
while(scanf("%s",s2),strcmp(s2,"END")) {
scanf("%s",s1);
insert(strlen(s1),root);
}
     scanf("%s",str);
getchar();
while(gets(str1),strcmp(str1,"END")) {
k=0;h[0]=0;
for(i=0;str1[i];i++) {
if(str1[i]>='a'&&str1[i]<='z')
str[k++]=str1[i];
else {
if(k==0) {
printf("%c",str1[i]);
continue;
}
str[k]=0;
find(strlen(str),root);
if(strlen(h)==0)
printf("%s",str);
else
printf("%s",h);
k=0;
h[0]=0;
printf("%c",str1[i]);
}
}
str[k]=0;
find(strlen(str),root);
if(strlen(h)==0)
printf("%s",str);
else
printf("%s",h);
printf(" ");
}
del(root);
return 0;
}
原文地址:https://www.cnblogs.com/thefirstfeeling/p/4410914.html