专栏首页csxiaoyaoHDU 1075 字典树 字符串匹配

HDU 1075 字典树 字符串匹配

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

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!

Hint

Huge input, scanf is recommended.

代码:

#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;
}

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • HDU 1023 高精度 卡特兰数

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot...

    csxiaoyao
  • HDU 1027 组合数学

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot...

    csxiaoyao
  • HDU 1002 高精度 大数据加法

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Ot...

    csxiaoyao
  • 10300 - Ecological Premium

    German farmers are given a premium depending on the conditions at their farmyard...

    若羽
  • HDOJ 1302(UVa 573) The Snail(蜗牛爬井)

    Problem Description A snail is at the bottom of a 6-foot well and wants to cli...

    谙忆
  • 聊聊flink LocalEnvironment的execute方法

    flink-java-1.6.2-sources.jar!/org/apache/flink/api/java/DataSet.java

    codecraft
  • 基于进化稀疏回归的杜芬振子实验辨识方法(CS.CE)

    本文提出了一种基于进化的稀疏回归算法,并将其应用于杜芬振子装置和数值模拟数据采集的实验数据。我们的目的是确定库仑摩擦项作为系统常微分方程的一部分。利用稀疏辨识对...

    用户7236395
  • 视频物体分割--One-Shot Video Object Segmentation

    One-Shot Video Object Segmentation CVPR2017 http://www.vision.ee.ethz.ch/~cvl...

    用户1148525
  • POJ 1905 Expanding Rods(二分)

    Expanding Rods Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 202...

    风骨散人Chiam
  • python 日志记录

    #!/bin/env python #--*-- coding=utf8 --*-- # # Author: ablozhou # E-mail: ab...

    py3study

扫码关注云+社区

领取腾讯云代金券