What Are You Talking About
Time Limit:5000MS     Memory Limit:204800KB     64bit IO Format:%I64d & %I64u

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!

就是把翻译的单词储存在val[ ]中需要注意开始START和结尾END

#include <cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef struct Trie_node
{
	int count;                    // 统计单词前缀出现的次数
	struct Trie_node* next[26];   // 指向各个子树的指针
	bool exist;
	char val[30];                // 标记该结点处是否构成单词
}TrieNode , *Trie;

TrieNode* createTrieNode()
{
	TrieNode* node = (TrieNode *)malloc(sizeof(TrieNode));
	node->count = 0;
	node->exist = false;
	memset(node->next , 0 , sizeof(node->next));    // 初始化为空指针
	return node;
}

void Trie_insert(Trie root, char* word,char *val)
{
	Trie node = root;
	char *p = word;
	int id;
	while( *p )
	{
		id = *p - 'a';
		if(node->next[id] == NULL)
		{
			node->next[id] = createTrieNode();
		}
		node = node->next[id];  // 每插入一步,相当于有一个新串经过,指针向下移动
		++p;
		node->count += 1;      // 这行代码用于统计每个单词前缀出现的次数(也包括统计每个单词出现的次数)
	}
	node->exist = true;
	strcpy(node->val,val);
	//printf("%s %s\n",word,node->val);   // 单词结束的地方标记此处可以构成一个单词
}

char* Trie_search(Trie root, char* word)
{
	Trie node = root;
	char *p = word;
	int id;
	while( *p )
	{
		id = *p - 'a';
		node = node->next[id];
		++p;
		if(node == NULL)
			return 0;
	}
	if(node->exist)
	return node->val;
	else return NULL;
}

char s1[3000],s2[3000];
int main()
{
    //freopen("cin.txt","r",stdin);
    Trie root = createTrieNode();
    int n,m;
    scanf("%s",s1);
    while(scanf("%s",s1) && strcmp(s1 , "END") != 0)
    {
        scanf("%s",s2);
        //printf("%s %s\n",s1,s2);
        Trie_insert(root,s2,s1);///
    }
    getchar();
    gets(s1);
    int k=0;
    while(gets(s1))
    {
        //if(strcmp(s1,"END")==0) break;
        if(s1[0]=='E') break;
        for(int i=0;s1[i]!='\0';i++)
        {

            if(s1[i]>='a'&&s2[i]<='z') s2[k++]=s1[i];
            else if(s1[i]!='E')
            {
                s2[k]='\0';
                char *p=Trie_search(root,s2);
                if(p) printf("%s",p);
                else printf("%s",s2);
                k=0;
                printf("%c",s1[i]);
            }

        }
      //  printf("%s",s1);
        printf("\n");
    }
    gets(s1);
    //printf("%s",s1);
    return 0;
}