What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 20448 Accepted Submission(s): 6766
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!HintHuge input, scanf is recommended.
感觉就是poj2503的加强版,本质上都一样。。。
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
#include <queue>
#include <iostream>
#include <assert.h>
#define INF 0x3f3f3f3f
using namespace std;
const int M = 3e6 + 10;
const int N = 3e6 + 10;
const int SIZE = 50;
struct Tri
{
int val[SIZE];
int cnt;
}ch[M];
char str1[N][50];
char str2[N];
char str3[N];
int tricnt;
int newnode()
{
memset(ch[tricnt].val, 0, sizeof(ch[tricnt].val));
ch[tricnt].cnt = 0;
return tricnt++;
}
void init()
{
tricnt = 0;
newnode();
}
void trinsert(char* s, int id)
{
int u = 0;
for (int i = 0; i < strlen(s); ++i) {
int index = s[i] - 'a';
if (!ch[u].val[index]) {
ch[u].val[index] = newnode();
}
u = ch[u].val[index];
}
ch[u].cnt = id;
}
int query(char* s, int st, int ed)
{
int u = 0;
for (int i = st; i <= ed; ++i) {
int index = s[i] - 'a';
if (!ch[u].val[index]) {
return 0;
}
u = ch[u].val[index];
}
return ch[u].cnt;
}
int main()
{
init();
int cascnt = 1;
while (gets(str3)) {
if (strcmp(str3, "START") == 0) continue;
if (strcmp(str3, "END") == 0) break;
int k = 0;
bool flag = 0;
for (int i = 0; str3[i]; i++) {
if (flag) {
str2[k++] = str3[i];
}
if (str3[i] == ' ') {
flag = 1;
str3[i] = '\0';
}
}
str2[k] = '\0';
trinsert(str2, cascnt);
strcpy(str1[cascnt++], str3);
}
while (gets(str3)) {
if (strcmp(str3, "START") == 0) continue;
if (strcmp(str3, "END") == 0) break;
int starts = 0;
for (int i = 0; str3[i]; i++) {
if (str3[i] < 'a' || str3[i] > 'z') {
int ans = query(str3, starts, i - 1);
if (ans == 0) {
for (int j = starts; j < i; j++) {
printf("%c", str3[j]);
}
}
else printf("%s", str1[ans]);
starts = i + 1;
printf("%c", str3[i]);
}
}
printf("\n");
}
return 0;
}