POJ2503字典树

此代码原始出处:http://blog.csdn.net/cnyali/article/details/47367403

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
	char word[15];
	int count;
	struct node *next[30];
	node(){
		count=0;
		memset(next,0,sizeof(next));
	}
};
void insert(struct node *p,char *s1,char *s2){
	int i,j,k,l=strlen(s1);
	struct node *q;
	for(i=0;i<l;i++){
		k=s1[i]-'a';
		if(p->next[k]==NULL){
			q=new node;
			p->next[k]=q;
		}
		p=p->next[k];
	}
	p->count=1;
	strcpy(p->word,s2);	
}
char *find(struct node *p,char *s){
	int i,j,k,l=strlen(s);
	for(i=0;i<l;i++){
		k=s[i]-'a';
		p=p->next[k];
		if(p==0)return NULL;
	}
	if(p->count==1)return p->word;
	else return NULL;
}
int main(){
	freopen("poj2503.in","r",stdin);
	freopen("poj2503.out","w",stdout);
	struct node *h;
	char s[15],s1[15],s2[15];
	int i,j,k,m,n;
	h=new node;
	while(1){		
		gets(s);
        if(s[0] == '\0')
            break;
        sscanf(s,"%s %s", s1, s2);
		insert(h,s2,s1);		
	}
	while(gets(s)!=NULL){
		if(s[0]=='\0')break;
		char *st=find(h,s);
		if(st!=0)
			puts(st);
		else
			puts("eh");
	}
	return 0;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值