1. 程式人生 > >HDU 1075 What Are You Talking About(經典字典樹)

HDU 1075 What Are You Talking About(經典字典樹)

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.

PS:很明顯,這是一個字典樹的題,把每個轉化單詞,放在翻譯單詞的最後一位,注意標記。

#include <iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<map>
#include<queue>
#include<set>
#include<cmath>
#include<stack>
#include<string>
const int maxn=2e5+5;
const int mod=1e9+7;
const int inf=1e9;
#define me(a,b) memset(a,b,sizeof(a))
typedef long long ll;
using namespace std;
struct node
{
	int flag;
	char word[30];
	node *next[30];
	node()
	{
		flag=0;
		me(next,0);
	}
};
node *root=new node();
void insert(char *s1,char *s2)
{
	node *p=root;
	for(int i=0;i<strlen(s1);i++)
	{
		if(p->next[s1[i]-'a']==NULL)
		{
			node *q=new node;
			p->next[s1[i]-'a']=q;
		}
		p=p->next[s1[i]-'a'];
	}
	p->flag=1;
	strcpy(p->word,s2);
}
char *find(char *s1)
{
    node *p=root;
    for(int i=0;i<strlen(s1);i++)
    {
        p=p->next[s1[i]-'a'];
        if(p==0)
            return NULL;
    }
    if(p->flag==1)
        return p->word;
    else
        return NULL;
}
int main()
{
	char s1[30],s2[30],s[3010];
	while(scanf("%s",&s1))
	{
		if(!strcmp(s1,"END"))
				break;
		if(!strcmp(s1,"START"))
				continue;
		scanf("%s",&s2);
		insert(s2,s1);
	}
	getchar();
	while(gets(s))
	{
		if(!strcmp(s,"END"))
                break;
		if(!strcmp(s,"START"))
				continue;
		for(int i=0;i<strlen(s);i++)
		{
            int m=0;
			while(s[i]>='a'&&s[i]<='z')
				s1[m++]=s[i++];
			s1[m]='\0';
			char *st=find(s1);
			if(st!=0)
				printf("%s",st);
			else
				printf("%s",s1);
			printf("%c",s[i]);///輸出符號
		}
		cout<<endl;
	}
	return 0;
}