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!
思路:
- 题目要求不高,map和Trie都可以做。
- 不过Trie的时间还是比map快了将近一倍。
- 提交结果如下:
map代码:
#include<bits/stdc++.h>
using namespace std;
#define MAXN 30000+10
map<string, string> mp;
map<string, string>::iterator iter;
bool word(char ch){
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
return true;
return false;
}
int main()
{
char e[20];
char s1[20], s2[20], s3[20], ch;
scanf("%s", e);//START
while(scanf("%s", s1), strcmp(s1, "END")){
scanf("%s", s2);
mp[s2] = s1;
}
scanf("%s", e);//START
int len = 0;
getchar();
while(ch = getchar()){
if (word(ch)){
s3[len++] = ch;
}
else{
s3[len] = '\0';
if (!strcmp(s3, "END")) break;
if (mp.count(s3)) cout<<mp[s3];
else cout<<s3;
len = 0;
printf("%c", ch);
}
}
return 0;
}
Trie代码:
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
bool word(char ch){
if (ch >= 'a' && ch <= 'z' || ch >= 'A' && ch <= 'Z')
return true;
return false;
}
typedef struct TrieNode{
char s[20];
bool flag;
TrieNode *next[26];
TrieNode(){
flag = false;
memset(next, 0, sizeof(next));
memset(s, 0, sizeof(s));
}
}TrieNode, *Trie;
TrieNode *InitTrieNode(){
TrieNode *p = new TrieNode;
return p;
}
void InsertNode(Trie root, char *word, char *ss){
Trie p = root;
char *ch = word;
int id;
while(*ch){
id = *ch - 'a';
if (p->next[id] == NULL)
p->next[id] = InitTrieNode();
p = p->next[id];
++ch;
}
p->flag = true;
strcpy(p->s, ss);
}
bool TrieSearch(Trie root, char *word, char *e){
Trie p = root;
char *ch = word;
int id;
bool ans = false;
while(*ch){
id = *ch - 'a';
if (p->next[id] == NULL)
return false;
p = p->next[id];
++ch;
}
strcpy(e, p->s);
if (p->flag) return true;
else return false;
}
int main()
{
Trie root = InitTrieNode();
char s1[20], s2[20], s3[20], ch;
scanf("%s", s1);//START
while(scanf("%s", s1), strcmp(s1, "END")){
scanf("%s", s2);
InsertNode(root, s2, s1);
}
scanf("%s", s1);//START
getchar();
int len = 0, ans;
while(ch = getchar()){
if (word(ch)){
s3[len++] = ch;
}
else{
s3[len] = '\0';
if (!strcmp(s3, "END")) break;
if (TrieSearch(root, s3, s2)) printf("%s", s2);
else printf("%s", s3);
len = 0;
printf("%c", ch);
}
}
return 0;
}