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.
Author
Ignatius.L
题目类型:trie树
算法分析:将外文单词插入到Trie树中,每个终止节点额外存储一个对应的英文单词,然后在文本中找到一个字符串ss,然后判断其是否在Trie树中,不在就直接输出ss,否则就输出这个终止节点所对应的英文单词
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> #define lson rt << 1, l, m #define rson rt << 1 | 1, m + 1, r using namespace std; const int INF = 0x7FFFFFFF; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int MOD = 1e9 + 7; const int maxn = 26; struct Trie { Trie *next[maxn]; char ss[maxn]; Trie () { memset (next, 0, sizeof (next)); memset (ss, 0, sizeof (ss)); } }; Trie *root; void Init () { root = new Trie (); } void Insert (char *s, char *bb) { if (!s[0]) return ; Trie *p = root; for (int i = 0; s[i]; i++) { if (!p -> next[s[i]-'a']) { p -> next[s[i]-'a'] = new Trie; } p = p -> next[s[i]-'a']; } strcpy (p -> ss, bb); } char res[maxn+66]; bool Query (char *s) { if (!s[0]) return false; Trie *p = root; for (int i = 0; s[i]; i++) { if (!p -> next[s[i]-'a']) return false; p = p -> next[s[i]-'a']; } if (p -> ss[0]) { strcpy (res, p -> ss); return true; } return false; } void DelTrie (Trie *t) { if (!t) return ; for (int i = 0; i < maxn; i++) { if (t -> next[i]) DelTrie (t -> next[i]); } delete t; } int main() { // freopen ("aaa.txt", "r", stdin); char aa[maxn+3000], bb[maxn+3000]; scanf ("%s", aa); Init (); while (scanf ("%s", aa) != EOF) { if (!strcmp (aa, "END")) break; scanf ("%s", bb); Insert (bb, aa); } scanf ("%s", aa); gets (aa); while (gets (aa)) { if (!strcmp (aa, "END")) break; char temp[maxn]; int len; for (int i = 0; aa[i]; ) { if (aa[i] >= 'a' && aa[i] <= 'z') { len = 0; while (aa[i] >= 'a' && aa[i] <= 'z') { temp[len++] = aa[i]; i++; } temp[len] = 0; if (Query (temp)) printf ("%s", res); else printf ("%s", temp); } else { printf ("%c", aa[i]); i++; } } puts (""); } return 0; } |
- « 上一篇:hdu1042
- hdu1085:下一篇 »