HDU 1075 What Are You Talking About(字典树)

                  What Are You Talking About

                                    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)

                                                             Total Submission(s): 9488    Accepted Submission(s): 2961

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!
 
 
方法一:字典树实现
  1 #include <stdio.h>
  2 #include <stdlib.h>
  3 #include <string.h>
  4 
  5 typedef struct _TrieTree
  6 {
  7     bool flag;
  8     char dictWord[11];
  9     struct _TrieTree *next[26];
 10 
 11 }TrieNode;
 12 
 13 TrieNode *CreateTrieTree()   //创建字典树
 14 {
 15     TrieNode *node = (TrieNode *)malloc(sizeof(TrieNode));
 16     node->flag = false;
 17     for(int i = 0; i < 26; i++)
 18         node->next[i] = NULL;
 19 
 20     return node;
 21 }
 22 
 23 void InsertTrieTree(TrieNode *root, char dictword[], char bookword[])
 24 {
 25     TrieNode *p = root;
 26     int length = strlen(bookword);
 27     for(int i = 0; i < length; i++)
 28     {
 29         int index = bookword[i] - 97;
 30         if(p->next[index] == NULL)
 31         {
 32             p->next[index] = CreateTrieTree();
 33         }
 34         p = p->next[index];
 35     }
 36 
 37     p->flag = true;
 38     strcpy(p->dictWord, dictword);
 39 }
 40 
 41 char * QueryTrieTree(TrieNode *root, char bookword[])
 42 {
 43     TrieNode *p = root;
 44     int length = strlen(bookword);
 45     for(int i = 0; i < length; i++)
 46     {
 47         int index = bookword[i] - 97;
 48         if(p->next[index] == NULL)
 49             return NULL;
 50 
 51         p = p->next[index];
 52     }
 53 
 54     if(p->flag)
 55         return p->dictWord;
 56     return NULL;
 57 }
 58 
 59 int main(int argc, char* argv[])
 60 {
 61     const int WORD_LEN = 11;
 62     char dictword[WORD_LEN];
 63     char bookword[WORD_LEN];
 64     char srartstr[] = "START";
 65 
 66     TrieNode *root = CreateTrieTree();
 67 
 68     scanf("%s", srartstr);
 69     while(scanf("%s", dictword) != EOF)
 70     {
 71         if(strcmp(dictword, "END") == 0)
 72             break;
 73 
 74         scanf("%s", bookword);
 75         InsertTrieTree(root, dictword, bookword);
 76     }
 77 
 78     const int MAX_LEN = 3005;
 79     char  linetext[MAX_LEN];
 80     char  searchWord[MAX_LEN];
 81     
 82     scanf("%s", srartstr);
 83     getchar();                        //屏蔽掉回车
 84     while(gets(linetext))
 85     {
 86         if(strcmp(linetext, "END") == 0)
 87             break;
 88 
 89         int k = 0;
 90         bool state = false;           //判断是否进行查询
 91         int length = strlen(linetext);
 92         for(int i = 0; i < length; i++)
 93         {
 94             if(linetext[i] >= 'a' && linetext[i] <= 'z')
 95             {
 96                 searchWord[k++] = linetext[i];
 97                 state = true;
 98             }
 99             else
100             {
101                 if(state == false)
102                 {
103                     printf("%c", linetext[i]);
104                     continue;
105                 }
106 
107                 searchWord[k] = '\0';
108                 k = 0;
109                 char *p = QueryTrieTree(root, searchWord);
110                 if(p == NULL)
111                     printf("%s", searchWord);
112                 else
113                     printf("%s", p);
114 
115                 state = false;
116                 printf("%c", linetext[i]);
117             }
118         }
119         printf("\n");
120     }
121 
122     return 0;
123 }

 

方法二:map实现

 1 #include <stdio.h>
 2 #include <string>
 3 #include <map>
 4 using namespace std;
 5 
 6 int main(int argc, char* argv[])
 7 {
 8     const int WORD_LEN = 11;
 9     char dictword[WORD_LEN];
10     char bookword[WORD_LEN];
11     char srartstr[] = "START";
12 
13     map<string, string> mapWord;
14 
15     scanf("%s", srartstr);
16     while(scanf("%s", dictword) != EOF)
17     {
18         if(strcmp(dictword, "END") == 0)
19             break;
20 
21         scanf("%s", bookword);
22         mapWord[bookword] = dictword;
23     }
24 
25     const int MAX_LEN = 3005;
26     char  linetext[MAX_LEN];
27     char  searchWord[MAX_LEN];
28 
29     scanf("%s", srartstr);
30     getchar();                        //屏蔽掉回车
31     while(gets(linetext))
32     {
33         if(strcmp(linetext, "END") == 0)
34             break;
35 
36         int k = 0;
37         bool state = false;           //判断是否进行查询
38         int length = strlen(linetext);
39         for(int i = 0; i < length; i++)
40         {
41             if(linetext[i] >= 'a' && linetext[i] <= 'z')
42             {
43                 searchWord[k++] = linetext[i];
44                 state = true;
45             }
46             else
47             {
48                 if(state == false)
49                 {
50                     printf("%c", linetext[i]);
51                     continue;
52                 }
53 
54                 searchWord[k] = '\0';
55                 k = 0;
56                 map<string, string>::iterator it = mapWord.find(searchWord);
57                 if(it != mapWord.end())
58                     printf("%s", it->second.c_str());
59                 else
60                     printf("%s", searchWord);
61 
62                 state = false;
63                 printf("%c", linetext[i]);
64             }
65         }
66         printf("\n");
67     }
68 
69     return 0;
70 }

 

转载于:https://www.cnblogs.com/Dreamcaihao/archive/2013/05/23/3095736.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值