Babelfish
Time limit:3000ms Memory limit:65536k
Total submissions:34278 accepted:14706
Description
You are just moved from Waterloo to a big city. The people speak an incomprehensible dialect of a foreign language. Fortunately, you had a dictionary to help you understand them.
Input
Input consists of 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words . Each dictionary entry are a line containing an 中文版 word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language and one word on each line. Each word in the input is a sequence of lowercase letters.
Output
Output is the message translated to 中文版, one word per line. Foreign words not in the dictionary should is translated as "eh".
Sample Input
Dog Ogday
Cat Atcay
Pig Igpay
Froot Ootfray
Loops Oopslay
Atcay
Ittenkay
Oopslay
Sample Output
Cat
Eh
Loops
Hint
Huge input and output,scanf and printf are recommended.
Source
Waterloo Local 2001.09.22
Title Link: poj.org/problem?id=2503
Topic: Give the corresponding relationship of the string, enter the second to find the corresponding first, there is no output eh
Topic Analysis: Dictionary tree or map, test instructions very naked, the corresponding relationship can be established, map words 2000ms+, dictionary tree 700ms+
Trie Tree:
#include <cstdio> #include <cstring>struct dic{char s1[15], s2[15]; int id;} D[100005];struct node{node *next[26]; BOOL End; int id; Node () {memset (Next, NULL, sizeof (next)); end = false; id =-1; }};void Insert (node *p, char *s, int id) {for (int i = 0; s[i]! = ' + '; i++) {int idx = s[i]-' a '; if (P-next[idx] = = NULL) P--NEXT[IDX] = new node (); p = P-NEXT[IDX]; } P-end = true; P-id = ID;} int Search (node *p, char *s) {for (int i = 0; s[i]! = ' s[i '; i++) {int idx =]-' a '; if (P-next[idx] = = NULL) return-1; p = P-NEXT[IDX]; } if (P-end) return P-id; return-1;} int main () {char s[30], get[15]; int cnt = 0; Node *root = new node (); while (gets (s) && strlen (s)) {d[cnt].id = cnt; SSCANF (S, "%s%s", D[CNT].S1, D[CNT].S2); Insert (Root, D[cNT].S2, CNT); cnt++; } while (scanf ("%s", get)! = EOF) {int id = Search (root, get); if (id = =-1) printf ("eh\n"); else printf ("%s\n", D[ID].S1); }}
Map
#include <iostream> #include <map> #include <cstring> #include <string> #include <cstdio> Using namespace Std;char s[30], s1[15], s2[15];string S3, Tmp;map <string, string> mp;map <string, string>:: Iterator It;int Main () {while (gets (s) && strlen (s)) { sscanf (S, "%s%s", S1, S2); MP[S2] = S1; } while (CIN >> S3) { it = Mp.find (S3); if (it = = Mp.end ()) cout << "eh" << Endl; else cout << It-second << Endl; }}
POJ 2503 Babelfish (trie tree or map)