hdu1075 What Are You Talking About-字典树

75 篇文章 0 订阅
4 篇文章 0 订阅

hdu1075 What Are You Talking About

 

 

模板是对了,但是题目的格式没弄清楚,然后换了一个主函数ac了。。。。

 

#include<iostream>
using namespace std;

struct trie
{
	bool isword;
	char word[11];
	trie *child[26];
	trie()
	{
		for(int i=0;i<26;i++) child[i]=0;
		isword=0;
		word[0]='\0';
	}
} root;

void insert(char *x,char *y)
{
	int k=0;trie *p=&root;
	while(y[k]!='\0')
	{
		if(!p->child[y[k]-'a']) p->child[y[k]-'a']=new trie;
		p=p->child[y[k++]-'a'];
	}
	p->isword=1;
	strcpy(p->word,x);
}

char *search(char *x)
{
	int k=0;trie *p=&root;
	while(x[k]!='\0'&&p->child[x[k]-'a']) p=p->child[x[k++]-'a'];
	if(x[k]=='\0'&&p->isword)
		return p->word;
	else
		return 0;
}

int main()
{
	int i,j,k,m,len;
	char x[11],y[11],tmp[3002];
	scanf("%s",x);
	while(scanf("%s",x))
	{
		if(strcmp(x,"END")==0) break;
		scanf("%s",y);
		insert(x,y);
	}
	scanf("%s",x);
	getchar();
	
	while(1)
	{
		gets(tmp);
		if(strcmp(tmp,"END") == 0 )	break;
		
		k=0;
		len = strlen(tmp);
		tmp[len]=' ';
		tmp[++len]='\0';
		
		
		for(i=0;i<len;i++)
			if(!(tmp[i]>='a' && tmp[i]<='z'))
			{
				x[k]='\0';
				char *temp = search(x);
				if(temp)
					printf("%s",temp);
				else
					printf("%s",x);
				k=0;
				if(i!=len-1)
					cout<<tmp[i];
			}
			else
			x[k++]=tmp[i];
		
		cout<<endl;
    }
	return 0;
} 


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值