What Are You Talking About

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

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
 
Recommend
We have carefully selected several similar problems for you:  1800 1671 1247 1298 1072 

 
  字典树,经典题,字典翻译
  WA了1次,后来发现是我把要翻译的单词搞错了,如果一个可以翻译的较长的单词包含着另一个可以翻译的较短的单词,这样遍历句子的时候就会先碰到较短的单词,WA的程序会先把这个短单词翻译出来,但实际上这是不对的,应该翻译整个较长的单词,而不是遇到什么就翻译什么。
  我的程序运行时间是281MS,用链表做的,用数组应该会快些。
  题意
  只有一组测试数据,分成两部分。第一部分是字典,给你若干个单词以及对应的翻译,以START开始,END结束;第二部分是翻译部分,给你若干句子,要求你根据上面给出的字典,将火星文翻译成英文,以START开始,END结束。这里翻译的时候,要注意只有字典中有对应翻译的单词才翻译,字典中没有对应翻译的单词以及标点符号空格原样输出。
  思路
  将字典中的火星文单词,也就是右边的单词构造成一棵字典树。在单词结束的节点中存储其对应的英文翻译。输入的时候读取整行,然后遍历每一个字符,将所有字母连续的记录到一个字符数组中,直到遇到一个非英文字母的字符为止,这个时候从字典树中查找这个单词有没有对应的英文翻译,如果有,输出这个翻译,如果没有,输出原来的单词。
  注意
  1.输入方法,我使用的scanf+gets。
  2.不要搞错要翻译的单词,遇到一个非字母的字符算一个单词。
  3.尽量不要使用cin,cout,容易超时
  代码
 #include <iostream>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
using namespace std; struct Tire{
Tire *next[];
char *trans; //定义一个指向一维数组的指针
Tire() //构造函数
{
int i;
for(i=;i<;i++){
next[i]=NULL;
}
trans = NULL;
}
}; Tire root; void Insert(char trans[],char word[]) //将单词word插入到字典树中,并在最后加上翻译trans
{
Tire *p = &root;
int i;
for(i=;trans[i];i++){
int n = trans[i]-'a';
if(p->next[n]==NULL)
p->next[n] = new Tire;
p = p->next[n];
}
p->trans = (char*)malloc(sizeof(char)*);
strcpy(p->trans,word);
}
void Find(char str[]) //找到对应的翻译并输出,没找到则输出原来的字符串
{
Tire *p = &root;
int i;
for(i=;str[i];i++){
int n = str[i]-'a';
if(p->next[n]==NULL){
printf("%s",str);
return ;
}
p = p->next[n];
}
if(p->trans==NULL)
printf("%s",str);
else
printf("%s",p->trans);
} int main()
{
char word[],trans[];
scanf("%s",word);
while(scanf("%s",word)!=EOF){ //输入字典
if(strcmp(word,"END")==) //遇到结束标记
break;
scanf("%s",trans);
Insert(trans,word); //将单词word插入到字典树中,并在最后加入其翻译
}
scanf("%s",word);
getchar();
char str[];
while(gets(str)){
if(strcmp(str,"END")==)
break;
int i,j=;
char t[]={};
for(i=;str[i];i++){
if('a'<=str[i] && str[i]<='z'){ //检测到的是小写字母
t[j++] = str[i];
}
else{
t[j] = '\0';
if(t[]!='\0'){ //不是空的
Find(t); //找到对应的翻译并输出,没找到则输出原来的字符串
t[]='\0',j=; //初始化t
}
printf("%c",str[i]);
}
}
printf("\n");
} return ;
}

Freecode : www.cnblogs.com/yym2013

hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)的更多相关文章

  1. HDU 1251 统计难题(字典树模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=1251 题意:给出一些单词,然后有多次询问,每次输出以该单词为前缀的单词的数量. 思路: 字典树入门题. #inc ...

  2. 字典树模板题(统计难题 HDU - 1251)

    https://vjudge.net/problem/HDU-1251 标准的字典树模板题: 也注意一下输入方法: #include<iostream> #include<cstdi ...

  3. 字典树&&01字典树专题&&对字典树的理解

    对于字典树和01字典树的一点理解: 首先,字典树建树的过程就是按照每个数的前缀来的,如果你要存储一个全小写字母字符串,那么这个树每一个节点最多26个节点,这样的话,如果要找特定的单词的话,按照建树的方 ...

  4. CH 1601 - 前缀统计 - [字典树模板题]

    题目链接:传送门 描述给定 $N$ 个字符串 $S_1,S_2,\cdots,S_N$,接下来进行 $M$ 次询问,每次询问给定一个字符串 $T$,求 $S_1 \sim S_N$ 中有多少个字符串是 ...

  5. hdu1305 字典树水题

    题意:      给你一些字符串,然后问你他们中有没有一个串是另一个串的前缀. 思路:       字典树水题,(这种水题如果数据不大(这个题目不知道大不大,题目没说估计不大),hash下也行,把每个 ...

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

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  7. HDU 1247 - Hat’s Words - [字典树水题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1247 Problem DescriptionA hat’s word is a word in the ...

  8. HDU 2072 - 单词数 - [(有点小坑的)字典树模板题]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2072 Problem Descriptionlily的好朋友xiaoou333最近很空,他想了一件没有 ...

  9. HDU 4825 Xor Sum(01字典树入门题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4825 题意: 给出一些数,然后给出多个询问,每个询问要从之前给出的数中选择异或起来后值最大的数. 思路:将给出的 ...

  10. HDU 1251 统计难题(字典树 裸题 链表做法)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

随机推荐

  1. Android实用代码模块集锦

    1. 精确获取屏幕尺寸(例如:3.5.4.0.5.0寸屏幕) 1 2 3 4 5 6 public static double getScreenPhysicalSize(Activity ctx)  ...

  2. maven简单配置

    maven-3.3.9下载 Maven是一个项目管理和综合工具.Maven提供了开发人员构建一个完整的生命周期框架.开发团队可以自动完成项目的基础工具建设,Maven使用标准的目录结构和默认构建生命周 ...

  3. 嵌套div中margin-top转移问题的解决办法

    在这两个浏览器中,有两个嵌套关系的div,如果外层div的父元素padding值为0,那么内层div的margin-top或者margin-bottom的值会“转移”给外层div. <!DOCT ...

  4. CentOS 下安装xdebug

    在CentOS 6.x 的系统中,是集成xdebug 的, yum install PHP-pecl-xdebug 如果是CentOS.5 也可能通过安装安装 epel 来安装 rpm -ivh ht ...

  5. 多节点 devstack 部署

    1, 网络配置 每个节点 /etc/network/interfaces auto eth0 iface eth0 inet static address 192.168.42.11 netmask ...

  6. 【SpringBoot】SpringBoot 入门示例

    参考资料: http://www.tuicool.com/articles/mqeee2A http://www.cnblogs.com/suncj/p/4065589.html http://spr ...

  7. SQL merge into 表合并

    Merge关键字是一个神奇的DML关键字.它在SQL Server 2008被引入,它能将Insert,Update,Delete简单的并为一句.MSDN对于Merge的解释非常的短小精悍:”根据与源 ...

  8. uniq 重复行统计

    uniq 命令   文字 uniq 是LINUX命令 用途 报告或删除文件中重复的行. 语法 uniq [ -c | -d | -u ] [ -f Fields ] [ -s Characters ] ...

  9. PHP带重试功能的curl

    2016年1月13日 10:48:10 星期三 /** * @param string $url 访问链接 * @param string $target 需要重试的标准: 返回结果中是否包含$tar ...

  10. static cross compile gtk-2.16.6+gtk-directfb+arm-linux (arm-linux-gcc-3.4.4+glib-2.3.5)

    ----------------------------------------------------------------------- In Ubuntu 10.4 Desktop and & ...