hdu1075(字典树入门)

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 18963    Accepted Submission(s): 6206


Problem Description
Ignatius is so lucky that he met a Martian yesterday. But he didn't know the language the Martians use. The Martian gives him a history book of Mars and a dictionary when it leaves. Now Ignatius want to translate the history book into English. Can you help him?
 

Input
The problem has only one test case, the test case consists of two parts, the dictionary part and the book part. The dictionary part starts with a single line contains a string "START", this string should be ignored, then some lines follow, each line contains two strings, the first one is a word in English, the second one is the corresponding word in Martian's language. A line with a single string "END" indicates the end of the directory part, and this string should be ignored. The book part starts with a single line contains a string "START", this string should be ignored, then an article written in Martian's language. You should translate the article into English with the dictionary. If you find the word in the dictionary you should translate it and write the new word into your translation, if you can't find the word in the dictionary you do not have to translate it, and just copy the old word to your translation. Space(' '), tab('\t'), enter('\n') and all the punctuation should not be translated. A line with a single string "END" indicates the end of the book part, and that's also the end of the input. All the words are in the lowercase, and each word will contain at most 10 characters, and each line will contain at most 3000 characters.
 

Output
In this problem, you have to output the translation of the history book.
 

Sample Input
  
  
START from fiwo hello difh mars riwosf earth fnnvk like fiiwj END START difh, i'm fiwo riwosf. i fiiwj fnnvk! END
 

Sample Output
  
  
hello, i'm from mars. i like earth!
//hdu1075(字典树)
//现在有一本火星字典;然后给你一句火星话,让你根据字典将其翻译成英文.
//解题思路:将字典中的火星词建一棵字典树.其中每个火星单词节点的value部分存放其对应英文单词的存放的位置.
//然后扫描一遍给出的火星话.若可找到对应单词,那么就翻译成英文.否则原样输出. 
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
struct Trie
{
	struct Trie *child[26];
	int value;
}*root;
int k;
char s[3000],map[1000000][12],s2[20],str[20];//其中s[]用来存放一句火星语.map[][]存放英文单词. 
void add(char s[],int k)   //建字典树函数 
{
	struct Trie *current,*newnode;
	int i,j;
	int len=strlen(s);
	current=root;
	for(i=0;i<len;i++)
	{
		if(current->child[s[i]-'a']!=NULL)    //该字母已存在,从下一个继续查找 
		{
			current=current->child[s[i]-'a'];
		}
		else                                //否则,建一个新节点. 
		{
			newnode = (struct Trie*)malloc(sizeof(struct Trie));
			newnode->value=-1;
			for(j=0;j<26;j++) newnode->child[j]=NULL;
			current->child[s[i]-'a']=newnode;
			current=newnode;
		}
	}
	current->value=k;                   //value存放其对应英文单词的位置. 
	return ;
}
int find(char s[])      //查找函数 
{
	int i,len;
	struct Trie *current;
	len=strlen(s);
	current=root;
	for(i=0;i<len;i++)
	{
		if(current->child[s[i]-'a']!=NULL)
		{
			current=current->child[s[i]-'a'];
		}
		else return -1;                //查找失败,返回-1. 
	}
	return current->value;           //返回对应英文单词的位置. 
}
int main()
{
	int i,j;
	k=0;
	root = (struct Trie*)malloc(sizeof(struct Trie));
	for(int i=0;i<26;i++)
	 root->child[i]=0;
	root->value=-1;
	memset(s2,0,sizeof(s2));
	
	//将接受的数据建成一颗字典树. 
	while(scanf("%s",map[k])&&strcmp(map[k],"END")!=0)
	{
		if(strcmp(map[k],"START")==0) continue;
		getchar();
		scanf("%s",s2);
		add(s2,k);      //单词s2[]加入树中。 
		k++;
	}
	getchar();
	while(gets(s)&&strcmp(s,"END")!=0)
	{
		if(strcmp(s,"START")==0) continue;
		int len=strlen(s);
		for(i=0,j=0;i<len;i++)
		{
			if('a'<=s[i]&&s[i]<='z')   //如果是字母就将其放入临时数组str[]中。 
			{
				str[j++]=s[i];
			}
			else 
			{
				str[j]='\0';       //字符数组最后没加'\0',结果一直TE.注意啊. 
				k=find(str);
				if(k!=-1) printf("%s",map[k]);
				else printf("%s",str);
				printf("%c",s[i]);
				j=0;
				memset(str,0,sizeof(str));  //注意清零. 
			}
		}
		printf("\n");
	}
	return 0;
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

bokzmm

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值