HDU 1075 What Are You Talking About

来源:互联网 发布:阿里巴巴的云计算 编辑:程序博客网 时间:2024/05/17 09:40

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

STARTfrom fiwohello difhmars riwosfearth fnnvklike fiiwjENDSTARTdifh, i'm fiwo riwosf.i fiiwj fnnvk!END

Sample Output

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

Hint

Huge input, scanf is recommended.

要翻译一段火星文
先给出一个对应表  "英文" - "火星文"
然后给一段火星文 照着对应表翻译 如果火星文不在对应表里 则输出火星文 否则输出英文 

Trie树的应用 注意处理好字符串细节 一大堆wa点 

#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <queue>#include <vector>#include <cmath>#include <stack>#include <string>#include <map>#include <set>#define pi acos(-1.0)#define LL long long#define ULL unsigned long long#define inf 0x3f3f3f3f#define INF 1e18#define lson l,mid,rt<<1#define rson mid+1,r,rt<<1|1#define mem0(a) memset(a, 0, sizeof(a))#define memi(a) memset(a, inf, sizeof(a))#define mem1(a) memset(a, -1, sizeof(a))using namespace std;typedef pair<int, int> P;const double eps = 1e-10;const int maxn = 3e6 + 5;const int mod = 1e8;const int N = 26;struct Node{int flag;char s[15];Node* next[N];}tree[maxn];int node = 0;Node* Creat(){Node* p = &tree[node++];p->flag = 0;for (int i = 0; i < N; i++)p->next[i] = NULL;return p;}void Insert(Node* root, char* s1, char* s2){Node* p = root;int i = 0, k;while (s1[i]){k = s1[i++] - 'a';if (p->next[k] == NULL)p->next[k] = Creat();p = p->next[k];}p->flag = 1;strcpy(p->s, s2);}int Search(Node* root, char* s1, char* s2){Node* p = root;int i = 0, k;while (s1[i]){k = s1[i++] - 'a';if (p->next[k] == NULL)return 0;p = p->next[k];}if (p->flag){strcpy(s2, p->s);return 1;}return 0;}int main(void){//freopen("C:\\Users\\wave\\Desktop\\NULL.exe\\NULL\\in.txt","r", stdin); int i, j, dex, len; char s1[15], s2[15], line[3005]; Node* root = Creat(); scanf("%s", &s1); // 输入 START  while (scanf("%s", &s1)){ if (strcmp(s1, "END") == 0) break;scanf("%s", &s2);Insert(root, s2, s1); } scanf("%s", line); // 输入START  getchar(); // 吸收回车  while (gets(line)){ if (strcmp(line, "END") == 0) break; len = strlen(line);memset(s1, 0, sizeof(s1));dex = 0;for (i = 0; i < len; i++){if (islower(line[i])){s1[dex++] = line[i];if (i == len-1){ // 当前行的末尾 直接查找 if (Search(root, s1, s2))printf("%s", s2);else printf("%s", s1);}}else {if (Search(root, s1, s2))printf("%s", s2);else printf("%s", s1);printf("%c", line[i]);dex = 0;memset(s1, 0, sizeof(s1));}}puts(""); }return 0;}


0 0
原创粉丝点击