Hdoj 1075

原题链接

描述

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?

输入

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.

输出

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

样例输入

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

样例输出

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

思路

字典树。
建树的时候把译文接在原文后面,标记译文开始。

代码

#include <bits/stdc++.h>
#define maxn 900000
using namespace std;

struct node
{
	int a[27];
	char c;
};

vector<node> trie;
int tot;

void find(char st[])
{
	int len1 = strlen(st);
	int len = len1;
	while(st[len-1] < 'a' || st[len-1] > 'z') len--;
	int u = 0;
	for(int i = 0; i < len; i++)
	{
		u = trie[u].a[st[i] - 'a'];
		if(u == 0) break;
	}
	if(u == 0) {printf("%s", st); return;}
	while(u != 0)
	{
		int f = 1;
		for(int i = 0; i < 26; i++)
			if(trie[u].a[i])
			{
				f = 0;
				printf("%c", i + 'a');
				u = trie[u].a[i];
				break;
			}
		if(trie[u].a[26] && f) {f = 0; printf("%c", trie[u].c); u = trie[u].a[26];}
		if(f) u = 0;
	}
	for(int i = len; i < len1; i++) printf("%c", st[i]);
}

void create(char st1[], char st2[])
{
	int len = strlen(st1);
	int u = 0;
	for(int i = 0; i < len; i++)
	{
		int v = st1[i] - 'a';
		if(v < 0 || v > 25) v = 26;
		if(trie[u].a[v] == 0)
		{
			if(v == 26) trie[u].c = st1[i];
			trie[u].a[v] = ++tot;
			node pnew; 
			for(int j = 0; j < 27; j++) pnew.a[j] = 0;
			trie.push_back(pnew);
		}
		u = trie[u].a[v];
	}
	len = strlen(st2);
	for(int i = 0; i < len; i++)
	{
		int v = st2[i] - 'a';
		if(v < 0 || v > 25) v = 26;
		if(trie[u].a[v] == 0)
		{
			if(v == 26) trie[u].c = st2[i];
			trie[u].a[v] = ++tot;
			node pnew; 
			for(int j = 0; j < 27; j++) pnew.a[j] = 0;
			trie.push_back(pnew);
		}
		u = trie[u].a[v];
	}
}

int main()
{
	char st1[12], st2[12]; scanf("%s", st1);
	node pnew; for(int j = 0; j < 27; j++) pnew.a[j] = 0;
	trie.push_back(pnew);
	while(scanf("%s %s\n", st1, st2))
		if(strcmp(st1, "END") == 0) break;
		else create(st2, st1);
	string st;
	while(getline(cin, st))
	{
		istringstream stream(st);
		int sum = 0;
        while(stream >> st1)
        {
        	sum ++;
        	if(strcmp(st1, "END") == 0) return 0;
			if(sum > 1) printf(" ");
			find(st1);
		}
		printf("\n");
	}
	return 0;
}
posted @ 2018-01-30 11:42  HackHarry  阅读(103)  评论(0编辑  收藏  举报