HDU1075 字典树

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
struct node{
	int count;
	char word[30];
	struct node *next[30];
	node(){
		count=0;
		memset(next,0,sizeof(next));
	}
};
struct node *h,*p,*q;
char w1[30],w2[30],s[3010];
void insert(char *s1,char *s2){
	int i,j,k;
	p=h;
	k=strlen(s1);
	for(i=0;i<k;i++){
		if(p->next[s1[i]-'a']==NULL){
			q=new node;
			p->next[s1[i]-'a']=q;
		}
		p=p->next[s1[i]-'a'];		
	}
	p->count=1;
	strcpy(p->word,s2);	
}
char * find(char *s1)
{
    int i,j,k;
	k=strlen(s1);
    p=h;
    for(i=0;i<k;i++)
    {
        p=p->next[s1[i]-'a'];
        if(p==0)
            return NULL;
    }
    if(p->count==1)
        return p->word;
    else
        return NULL;
}
int main(){
	int i,j,k,k1,m,n;
	scanf("%s",&w1);
	h=new node;
	while(1){
		scanf("%s",&w1);
		if(strcmp(w1,"END")==0)break;
		scanf("%s",&w2);
		insert(w2,w1);		
	}
	scanf("%s",&w1);
	getchar();
	while(1){
		gets(s);
		if(strcmp(s,"END")==0)break;
		k=strlen(s);
		for(i=0;i<k;i++){
			m=0;
			while(s[i]>='a' && s[i]<='z'){
				w1[m++]=s[i];
				i++;
			}
			w1[m]='\0';
			//把W1翻译过来;
			char *st=find(w1);
			if(st!=0)
				printf("%s",st);
			else
				printf("%s",w1);
			//翻译完毕;
			printf("%c",s[i]);		
		}
		puts("");
	}
	return 0;
}

  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值