额,跟hdu的1075很像,不过比那题简单多了,就是读入时有点麻烦,用到了一个函数
具体看代码吧,这题没什么,不过我的内存开了太多了,不解……
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct node
{node *next[26];int v;char str[15];
}*tree,t;
tree root;
void insert(char *s,char *s1)
{tree p=root,newnode;for(;*s1!='\0';s1++){int d=*s1-'a';if(p->next[d]!=NULL)p=p->next[d];else {newnode=(tree)malloc(sizeof(t));for(int i=0;i<26;i++)newnode->next[i]=NULL;newnode->v=-1;newnode->str[0]=0;p->next[d]=newnode;p=newnode;}}p->v=1;strcpy(p->str,s);
}
void search(char *s)
{tree p=root;for(;*s!='\0';s++){int d=*s-'a';if(p->next[d]!=NULL)p=p->next[d];}if(p->v==1)puts(p->str);else printf("eh\n");return ;}
int main()
{char s1[15],s2[15],s3[15];char ch[25];root=(tree)malloc(sizeof(t));for(int i=0;i<26;i++)root->next[i]=NULL;root->v=-1;root->str[0]=0;while(gets(ch)!=NULL && ch[0]) {sscanf(ch, "%s%s", s1, s2);//用到了这个函数,简化了许多insert(s1, s2);}while(gets(s3)){if(strcmp(s3,"")==0)break;//puts(s3);search(s3);}return 0;
}