HDU 1075 What Are You Talking About (Trie树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075
map可以过。。。我上的字典树,小bug有点尴尬,题目没有明确给出数据范围也是无奈。
贡献了几次RE 一次WA。尴尬。discuss里面有个说注意前缀的到是给了点tip。总体来说不错
代码:
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <functional> 3 #include <algorithm> 4 #include <iostream> 5 #include <cstring> 6 #include <cassert> 7 #include <cstdio> 8 #include <cctype> 9 #include <vector> 10 #include <string> 11 #include <queue> 12 #include <stack> 13 #include <cmath> 14 #include <map> 15 #include <set> 16 using namespace std; 17 #define rep(i,a,n) for (int i=a;i<n;i++) 18 #define per(i,a,n) for (int i=n-1;i>=a;i--) 19 #define pb push_back 20 #define mp make_pair 21 #define all(x) (x).begin(),(x).end() 22 #define fi first 23 #define se second 24 #define SZ(x) ((int)(x).size()) 25 typedef vector<int> VI; 26 typedef long long ll; 27 typedef pair<int, int> PII; 28 const ll mod = 1000000007; 29 ll powmod(ll a, ll b) { ll res = 1; a %= mod; assert(b >= 0); for (; b; b >>= 1) { if (b & 1)res = res*a%mod; a = a*a%mod; }return res; } 30 // head 31 const int inf = 0x3f3f3f3f; 32 #define maxn 15 33 #define maxnode 26 34 int trie[500000][maxnode]; 35 int val[500000][maxnode]; 36 int totid = 1; 37 38 void init(){ 39 memset(trie, 0, sizeof(trie)); 40 memset(val, -1, sizeof(val)); 41 totid = 1; 42 } 43 void insert_node(char *x, int y){ 44 int p = 1, len = strlen(x); 45 for(int i = 0; i < len - 1; i++){ 46 if(trie[p][x[i] - 'a']){ 47 p = trie[p][x[i] - 'a']; 48 } 49 else{ 50 p = trie[p][x[i] - 'a'] = ++totid; 51 } 52 } 53 val[p][x[len - 1] - 'a'] = y; 54 return; 55 } 56 int find_node(char *x){ 57 int p = 1, len = strlen(x); 58 int tmval = -1; 59 for(int i = 0; i < len; i++){ 60 if(i == len - 1){ 61 tmval = val[p][x[i] - 'a']; 62 return tmval; 63 } 64 if(trie[p][x[i] - 'a']){ 65 p = trie[p][x[i] - 'a']; 66 } 67 else{ 68 return tmval; 69 } 70 } 71 return tmval; 72 } 73 74 char dict[500005][maxn]; 75 char book[500005], chn[maxn],eng[maxn]; 76 77 int main(){ 78 scanf("%s", chn); 79 int ttid = 0; 80 init(); 81 while(scanf(" %s", chn) && strcmp(chn, "END") != 0){ 82 strcpy(dict[ttid], chn); 83 scanf(" %s",eng); 84 insert_node(eng, ttid++); 85 } 86 scanf("%s", chn); 87 getchar(); 88 while(gets(book) && strcmp(book, "END") != 0){ 89 int len = strlen(book); 90 for(int i = 0; i < len;){ 91 while(!isalpha(book[i]) && book[i]) { 92 printf("%c", book[i]); 93 i++; 94 } 95 int j = i; 96 while(isalpha(book[j])) j++; 97 char tmx[maxn]; 98 strncpy(tmx, book + i, j - i); 99 tmx[j - i] = '\0'; 100 int tmf = find_node(tmx); 101 if(tmf != -1){ 102 printf("%s", dict[tmf]); 103 } 104 else{ 105 for(int k = i; k < j; k++) 106 printf("%c", book[k]); 107 } 108 i = j; 109 } 110 puts(""); 111 } 112 } 113 /* 114 START 115 from fiwo 116 hello difh 117 trap dif 118 mars riwosf 119 earth fnnvk 120 like fiiwj 121 END 122 START 123 difh, i'm fiwo riwosf. 124 i fiiwj fnnvk! dif fnn 125 END 126 */
题目:
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 22781 Accepted Submission(s): 7604
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