前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDU 1075 字典树 字符串匹配

HDU 1075 字典树 字符串匹配

作者头像
csxiaoyao
发布2019-02-18 17:56:03
5020
发布2019-02-18 17:56:03
举报
文章被收录于专栏:csxiaoyao

What Are You Talking About

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

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

代码语言:javascript
复制

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

Sample Output

代码语言:javascript
复制

hello, i'm from mars. i like earth!

Hint

Huge input, scanf is recommended.

代码:

代码语言:javascript
复制
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<ctype.h>
struct dictree
{
	dictree *child[26];
	char aim[15];
	int flag;
}*root;
char ans[15];
int flag;
void insert(char*source,char*temp)
{
	int i,j;
	dictree *newnode,*current;
	current=root;
	for(i=0;i<strlen(source);i++)
	{
		if(current->child[source[i]-'a']!=0)
			current=current->child[source[i]-'a'];
		else
		{
			newnode=new dictree;
			newnode->flag=0;
			for(j=0;j<26;j++)
				newnode->child[j]=0;
			current->child[source[i]-'a']=newnode;
			current=newnode;
		}
	}
	current->flag=1;
	strcpy(current->aim,temp);
}
void find(char*source)
{
	dictree *current;
	int i;
	current=root;
	for(i=0;i<strlen(source);i++)
	{
		if(current->child[source[i]-'a']!=0)
			current=current->child[source[i]-'a'];
		else
		{
			flag=1;
			return;
		}
	}
	if(current->flag==0)
	{
		flag=1;
		return;
	}
	strcpy(ans,current->aim);
}
int main()
{
	int i,j;
	char str1[15],str2[15],str[3300],temp[15];
	scanf("%s",str1);
	root=new dictree;
	for(i=0;i<26;i++)
		root->child[i]=0;
	root->flag=0;
	while(scanf("%s",str1)&&strcmp(str1,"END")!=0)
	{
		scanf("%s",str2);
		insert(str2,str1);
	}
	scanf("%s",str);
	getchar();
	while(gets(str)&&strcmp(str,"END")!=0)
	{
		j=0;
		for(i=0;i<strlen(str);i++)
		{
			if(islower(str[i]))
				temp[j++]=str[i];
			else
			{
				if(j)
				{
					temp[j]=0;
					flag=0;
					find(temp);
					if(flag)
						printf("%s",temp);
					else
						printf("%s",ans);
				}
				if(str[i])
					printf("%c",str[i]);
				j=0;
			}
		}
		printf("\n");
	}
	return 0;
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2014年02月17日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • What Are You Talking About
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档