HDU 1075 map or 字典树

What Are You Talking About

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)
Total Submission(s): 12773    Accepted Submission(s): 4069

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!

题目意思大概是一种语言和英语对应,首先给你几行对应的语言和英语,以START开始END结束,每一行前面的单词为英语,后面为对应该英语的语言。

然后给你几行用该语言写的句子,让你用翻译成英语输出。

看过题后,想到了两种方法,一种是用map函数存储语言和英语的映射关系从而翻译句子。第二种是字典树,建立字典树把该语言存放到字典树中,然后翻译的时候就是查找的过程。

map函数代码:

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <iostream>
 4 #include <map>
 5 #include <algorithm>
 6 using namespace std;
 7
 8 main()
 9 {
10     map<string,string>ma;
11     char a[15], b[15], s[3005];
12     int n, i, j, k;
13     scanf("START");
14     while(scanf("%s",a)&&strcmp(a,"END")!=0)
15     {
16         scanf("%s",b);
17         ma[b]=a;
18     }
19     scanf("%*s");
20     getchar();
21     while(gets(s)&&strcmp(s,"END")!=0)
22     {
23         k=0;n=strlen(s);
24         for(i=0;i<n;i++)
25         {
26             if(s[i]>=‘a‘&&s[i]<=‘z‘)
27             b[k++]=s[i];
28             else{
29                 b[k]=‘\0‘;
30                 k=0;
31                 if(ma.find(b)!=ma.end())
32                 cout<<ma[b];
33                 else cout<<b;
34                 printf("%c",s[i]);
35             }
36         }
37         if(s[i-1]>=‘a‘&&s[i-1]<=‘z‘)
38         {
39             b[k]=‘\0‘;
40             cout<<ma[b];
41         }
42         printf("\n");
43     }
44
45 }

字典树代码:

 1 #include <stdio.h>
 2 #include <algorithm>
 3 #include <string.h>
 4 #include <iostream>
 5 using namespace std;
 6
 7 char a[1000005][15];
 8
 9 struct node{
10     int id;
11     struct node *next[26];
12     node(){
13         id=-1;
14         memset(next,0,sizeof(next));
15     }
16 }root;
17
18
19 void insert(char *s,int id)
20 {
21     struct node *p;
22     p=&root;
23     int i=0;
24     while(s[i])
25     {
26         if(p->next[s[i]-‘a‘]==0) {
27             p->next[s[i]-‘a‘]=new node;
28             p=p->next[s[i]-‘a‘];
29         }
30         else p=p->next[s[i]-‘a‘];
31         i++;
32     }
33     p->id=id;
34 }
35
36
37 char *find(char *s)
38 {
39     struct node *p;
40     p=&root;
41     int i=0;
42     while(s[i]&&p->next[s[i]-‘a‘])
43     {
44         p=p->next[s[i]-‘a‘];
45         i++;
46     }
47     if(!s[i]&&p->id!=-1)
48     return a[p->id];
49     else return s;
50 }
51
52 main()
53 {
54     char b[3005], c[3005];
55     int i, j, k=0;
56     scanf("START");
57     while(scanf("%s",a[k])&&strcmp(a[k],"END")!=0)
58     {
59         scanf("%s",b);
60         insert(b,k);
61         k++;
62     }
63     scanf("%s%*c",c);
64     while(gets(b))
65     {
66         if(strcmp(b,"END")==0) break;
67         i=0;    k=0;
68         for(i=0;b[i];i++)
69         {
70             if(b[i]>=‘a‘&&b[i]<=‘z‘)
71             c[k++]=b[i];
72             else{
73                 c[k]=‘\0‘;
74                 k=0;
75                 printf("%s",find(c));
76                 printf("%c",b[i]);
77             }
78         }
79         if(b[i-1]>=‘a‘&&b[i-1]<=‘z‘)
80         {
81             c[k]=‘\0‘;
82             printf("%s",find(c));
83         }
84
85         printf("\n");
86     }
87 }

对于本题,字典树效率比map高出很多。

HDU 1075 map or 字典树

时间: 2024-12-14 01:53:20

HDU 1075 map or 字典树的相关文章

HDU 4825 Xor Sum 字典树+位运算

点击打开链接 Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total Submission(s): 291    Accepted Submission(s): 151 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus

hdu 1251 统计难题 字典树

// hdu 1251 统计难题 字典树 // // 题目大意: // // 有一系列的单词表,以空行结尾,之后会有一些字母串,找出以这些字符串 // 作为前缀的单词的个数 // // // 解题思路: // // 字典树 Trie,在插入字符串的时候每遇到一个节点,该节点的值++.查找的时候 // 字符串时,如果找到了,那么返回当前的val,否则返回0,因为没有以这个字符串 // 为前缀的单词. // // // 感悟: // // 这段时间想学学数据结构,就看了看刘老的大白书,感觉用数组挺巧

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

What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K (Java/Others)Total Submission(s): 24624    Accepted Submission(s): 8280 Problem Description Ignatius is so lucky that he met a Martian yesterday. But

ACM学习历程—HDU 4287 Intelligent IME(字典树 || map)

Description We all use cell phone today. And we must be familiar with the intelligent English input method on the cell phone. To be specific, the number buttons may correspond to some English letters respectively, as shown below: 2 : a, b, c    3 : d

HDU 4287 Intelligent IME (字典树 &amp;&amp; map)

分析:此题题意很简单,我就不说了,第一想到的就是用字典树做,首先你得考虑用哪个作为字典树,显然,这里用后面的字符串作为树更好. 接着就是套模板了.此题WA了无数次,找bug找了一天,也没找到头绪,后来看别人的结题报告,才明白,原来坑爹的把memset(mark)放在了循环外面,一直以为只循环一次就够了,,真是坑爹啊,,, 做到现在了,感觉字典树的问题基本上都能用map来解决. 代码一:字典树 学会了用内联,确实会快一些 #include <iostream> #include <cstr

HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input 输入数据的第一部分是一张单词表,每行一个单词,单词的长度不超过10,它们代表的是老师交给Ignatius统计的单词,一个空行代表单词表的结束.第二部分是一连串的提问,每行一个提问,每个提问都是一个字符串. 注意:本题只有一组测试数据,处理到文件结束. Output 对于每个提

hdu 5536 Chip Factory 字典树+bitset 铜牌题

Chip Factory Time Limit: 18000/9000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)Total Submission(s): 1842    Accepted Submission(s): 833 Problem Description John is a manager of a CPU chip factory, the factory produces lots of chip

hdu 1251 统计难题 (字典树入门题)

1 /******************************************************* 2 题目: 统计难题 (hdu 1251) 3 链接: http://acm.hdu.edu.cn/showproblem.php?pid=1251 4 算法: 字典树 5 提示: 这题压要用c++提交,G++会超内存 6 *******************************************************/ 7 #include<cstdio> 8

HDU 1671 Phone List(字典树Trie)

解题思路: 判断是否有一个字符串是另一个字符串的前缀,直接用字典树搞. #include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <algorithm> #include <cmath> #include <queue> #include <set> #include <stack> #i