HDU 1075 What Are You Talking About (字典树)
2017-07-10 19:59
447 查看
原题
What Are You Talking About
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 23473 Accepted Submission(s): 7867
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
STARTfrom 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.
题意
输入分为两个部分,每个部分以“START”开头,以“END”结尾。第一部分为“英语-火星文字典”,第二部分为要你翻译的火星文,字典中查不到的单词、空格、水平制表符标点符号等不用翻译。涉及知识及算法
字典树,详情参见百度百科。提示:用火星文单词建立字典树,将翻译存在每个单词的字母结点上。遍历句子,碰到可译单词就把翻译取出,回到树根;碰到不可译字符就直接输出,继续遍历,重复上述步骤。
代码
#include<iostream> #include<cstring> #include<cstdio> #include<malloc.h> using namespace std; typedef struct Trie_node { int count; // 统计单词前缀出现的次数 struct Trie_node* next[26]; // 指向各个子树的指针 bool exist; // 标记该结点处是否构成单词 char trans[11]; // 翻译 }TrieNode , *Trie; TrieNode* createTrieNode() { TrieNode* node = (TrieNode *)malloc(sizeof(TrieNode)); node->count = 0; node->exist = false; memset(node->next , 0 , sizeof(node->next)); // 初始化为空指针 return node; } void Trie_insert(Trie root, char* word , char* trans) { Trie node = root; char *p = word; int id; while( *p ) { id = *p - 'a'; if(node->next[id] == NULL) { node->next[id] = createTrieNode(); } node = node->next[id]; // 每插入一步,相当于有一个新串经过,指针向下移动 ++p; node->count += 1; // 这行代码用于统计每个单词前缀出现的次数(也包括统计每个单词出现的次数) } node->exist = true; // 单词结束的地方标记此处可以构成一个单词 strcpy(node->trans , trans); } char* Trie_search(Trie root, char* word) { Trie node = root; char *p = word; int id; while( *p ) { id = *p - 'a'; node = node->next[id]; ++p; if(node == NULL) return 0; } if(node->exist) // 查找成功 return node->trans; else // 查找失败 return NULL; } int main(void) { Trie root = createTrieNode(); // 初始化字典树的根节点 char str1[3003] , str2[3003] , str[3003] , *p; int i , k; scanf("%s",str1); while(scanf("%s",str1) && strcmp(str1 , "END") != 0) { scanf("%s",str2); Trie_insert(root , str2 , str1); } getchar(); gets(str1); k = 0; while(gets(str1)) { if(strcmp(str1 , "END") == 0) break; for(i = 0 ; str1[i] != '\0' ; ++i) { if(str1[i] >= 'a' && str1[i] <= 'z') { str[k++] = str1[i]; } else { str[k] = '\0'; p = Trie_search(root , str); if(p) printf("%s", p); else printf("%s", str); k = 0; printf("%c", str1[i]); } } printf("\n"); } return 0; }
文章转载自CSDN博主hackbuteer1,附上链接http://blog.csdn.net/hackbuteer1/article/details/7964147,向他表示感谢。
相关文章推荐
- HDU 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About(字典树)
- HDU 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About(字典树)
- HDU1075 What Are You Talking About 解题报告--字典树
- (HDU)1075 - What Are You Talking About【字典树】or【字符串map】
- HDU 1075 What Are You Talking About 经典字典树
- HDU 1075 What Are You Talking About (字典树)
- 【字典树】hdu 1075 What Are You Talking About
- HDU - 1075 What Are You Talking About(字典树)
- 【HDU 1075】What Are You Talking About(字典树)
- hdu 1075 What Are You Talking About (字典树)
- hdu 1075 What Are You Talking About 字典树 trie
- hdu 1075 What Are You Talking About 字典树
- HDU 1075 What Are You Talking About (map 或者 字典树)
- HDU_1075_What Are You Talking About(字典树)
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)