[HDU 1075]What Are You Talking About

Posted kakakakakaka

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[HDU 1075]What Are You Talking About相关的知识,希望对你有一定的参考价值。

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!

题解:

裸的trie树,很早就做过了的题目,发到博客记录一下。

  1 //Never forget why you start
  2 #include<iostream>
  3 #include<cstring>
  4 using namespace std;
  5 
  6 typedef struct Trie_node
  7 {
  8     int count; 
  9     struct Trie_node* next[26];
 10     bool exist;      
 11     char trans[11];       
 12 }TrieNode , *Trie;
 13 
 14 TrieNode* createTrieNode()
 15 {
 16     TrieNode* node = (TrieNode *)malloc(sizeof(TrieNode));
 17     node->count = 0;
 18     node->exist = false;
 19     memset(node->next , 0 , sizeof(node->next)); 
 20     return node;
 21 }
 22 
 23 void Trie_insert(Trie root, char* word , char* trans)
 24 {
 25     Trie node = root;
 26     char *p = word;
 27     int id;
 28     while( *p )
 29     {
 30         id = *p - a;
 31         if(node->next[id] == NULL)
 32         {
 33             node->next[id] = createTrieNode();
 34         }
 35         node = node->next[id];
 36         ++p;
 37         node->count += 1;        }
 38     node->exist = true;      
 39     strcpy(node->trans , trans);
 40 }
 41 
 42 char* Trie_search(Trie root, char* word)
 43 {
 44     Trie node = root;
 45     char *p = word;
 46     int id;
 47     while( *p )
 48     {
 49         id = *p - a;
 50         node = node->next[id];
 51         ++p;
 52         if(node == NULL)
 53             return 0;
 54     }
 55     if(node->exist)
 56         return node->trans;
 57     else                 
 58         return NULL;
 59 }
 60 
 61 int main(void)
 62 {
 63     Trie root = createTrieNode();   
 64     char str1[3003] , str2[3003] , str[3003] , *p;
 65     int i , k;
 66 
 67     scanf("%s",str1);
 68     while(scanf("%s",str1) && strcmp(str1 , "END") != 0)
 69     {
 70         scanf("%s",str2);
 71         Trie_insert(root , str2 , str1);
 72     }
 73 
 74     getchar();
 75     gets(str1);
 76     k = 0;
 77     while(gets(str1))
 78     {
 79         if(strcmp(str1 , "END") == 0)
 80             break;
 81         for(i = 0 ; str1[i] != \0 ; ++i)
 82         {
 83             if(str1[i] >= a && str1[i] <= z)
 84             {
 85                 str[k++] = str1[i];
 86             }
 87             else
 88             {
 89                 str[k] = \0;
 90                 p = Trie_search(root , str);
 91                 if(p)
 92                     printf("%s", p);
 93                 else
 94                     printf("%s", str);
 95                 k = 0;
 96                 printf("%c", str1[i]);
 97             }
 98         }
 99         printf("\n");
100     }
101 
102     return 0;
103 }

一直都不知道哪里错了的代码:

 1 //Never forget why you start
 2 #include<iostream>
 3 #include<cstdio>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cmath>
 7 #include<algorithm>
 8 using namespace std;
 9 struct trie_node{
10   int count;
11   char trans[120];
12   trie_node* child[26];
13   bool exist;
14 }trienode,*root;
15 trie_node* create_trie_node(){
16   trie_node* node=(trie_node*)malloc(sizeof(trienode));
17   node->count=0;
18   node->exist=0;
19   memset(node->child,0,sizeof(node->child));
20   return node;
21 }
22 void trie_insert(char word[],char tran[]){
23   int p=0,id,len=strlen(word);
24   trie_node* r=root;
25   while(p<len){
26     id=word[p]-a;
27     if(r->child[id]==NULL)r->child[id]=create_trie_node();
28     r=r->child[id];
29     r->count++;
30     p++;
31   }
32   r->exist=1;
33   memcpy(r->trans,tran,sizeof(r->trans));
34 }
35 char ss[10001];
36 bool search(char word[]){
37   int p=0,id,len=strlen(word);
38   trie_node* r=root;
39   while(p<len){
40     id=word[p]-a;
41     if(r->child[id]==NULL)return 0;
42     r=r->child[id];
43     p++;
44   }
45   if(r->exist){memcpy(ss,r->trans,sizeof(ss));return 1;}
46   else return 0;
47 }
48 char s[10001],t[10001];
49 int main(){
50   root=create_trie_node();
51   int i,j;
52   scanf("%s",s);
53   while(1){
54     scanf("%s",s);
55     if(s[0]==E)break;
56     else{
57       scanf("%s",t);
58       trie_insert(t,s);
59     }
60   }
61   scanf("%s",s);
62   char ch;
63   while(1){
64     scanf("%s",s);ch=getchar();
65     if(s[0]==E)break;
66     else{
67       int len=strlen(s);
68       char f=a;
69       if(s[len-1]<a||s[len-1]>z){f=s[len-1];s[len-1]=0;}
70       if(search(s)){printf("%s",ss);if(f!=a)printf("%c",f);printf(" ");}
71       else {printf("%s",s);if(f!=a)printf("%c",f);printf(" ");}
72     }
73     if(ch==\n)printf("\n");
74   }
75   return 0;
76 }

 

以上是关于[HDU 1075]What Are You Talking About的主要内容,如果未能解决你的问题,请参考以下文章

HDU 1075 What Are You Taking About

hdu 1075 What Are You Talking About

hdu 1075 What Are You Talking About (map)

HDU 1075 What Are You Talking About (Trie)

HDU1075 What Are You Talking About STL

HDU 1075 - What Are You Talking About