What Are You Talking About

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

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.

 

题目链接:HDU 1075

结构体Trie中用flag=1表示当前点是否是某个单词的结尾处(防止出现单词a为单词b的前缀但是却被b覆盖的情况,比如diff对应bbb,但是此时diff的前缀dif显然是不存在的),若为结尾,则flag赋值为1,然后把对应的翻译单词赋值给这个节点的s[]。

查询的是否看查完之后的flag是否为1,如果为1则存在该单词,否则flag为0或根本查不到,则返回NULL

一开始智障了把每个点flag都设为1狂WA……

代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define CLR(x,y) memset(x,y,sizeof(x))
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
typedef pair<int,int> pii;
typedef long long LL;
const double PI=acos(-1.0);
const int N=26;
const int M=100;
struct Trie
{
Trie *nxt[N];
int flag;
char s[M];
Trie()
{
CLR(nxt,0);
CLR(s,0);
flag=0;
}
};
Trie *L=new Trie();
void update(char s[],char earth[])
{
int len=strlen(s);
int indx;
Trie *cur=L;
for (int i=0; i<len; ++i)
{
indx=s[i]-'a';
if(cur->nxt[indx])
cur=cur->nxt[indx];
else
{
Trie *one=new Trie();
cur->nxt[indx]=one;
cur=one;
}
}
strcpy(cur->s,earth);
cur->flag=1;
}
char* Find(char s[])
{
int len=strlen(s);
int indx;
Trie *cur=L;
for (int i=0; i<len; ++i)
{
indx=s[i]-'a';
if(cur->nxt[indx]==NULL)
return NULL;
cur=cur->nxt[indx];
}
return cur->flag?cur->s:NULL;
}
char from[M],to[M];
char tot[1000010];
char temp[M];
int main(void)
{
int i;
scanf("%s",from);
getchar();
while (~scanf("%s",to)&&strcmp(to,"END"))
{
scanf("%s",from);
update(from,to);
}
getchar(); gets(from);
while (gets(tot)&&strcmp(tot,"END"))
{
int len=strlen(tot);
int k=0;
CLR(temp,0);
for (i=0; i<len; ++i)
{
if(islower(tot[i]))
temp[k++]=tot[i];
else
{
char *one=Find(temp);
if(k)
printf("%s",one==NULL?temp:one);
putchar(tot[i]);
CLR(temp,0);
k=0;
}
}
if(k)
{
char *one=Find(temp);
printf("%s",one==NULL?temp:one);
}
putchar('\n');
}
return 0;
}

HDU 1075 What Are You Talking About(Trie的应用)的更多相关文章

  1. HDU 1075 What Are You Talking About (Trie)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  2. 题解报告:hdu 1075 What Are You Talking About

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 Problem Description Ignatius is so lucky that he ...

  3. hdu 1075 (map)

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 What Are You Talking About Time Limit: 10000/5000 MS ...

  4. hdu 1075 What Are You Talking About

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意:比较简单,易懂,这里不做说明. 解法:第一种方法:用map映射,耗时1000+ms:第二种 ...

  5. HDU 1057 What Are You Talking About trie树 简单

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 题意 : 给一个单词表然后给一些单词,要求翻译单词表中有的单词,没有则直接输出原单词. 翻译文段部分get ...

  6. 字典树 HDU 1075 What Are You Talking About

    http://acm.hdu.edu.cn/showproblem.php?pid=1075 ;}

  7. HDU 1075-What Are You Talking About(Trie)

    题意: 给你一个字典 一个英文单词对应一个火星单词 给你一段火星文翻译成英文 字典上的没有的不翻译 分析: 没有给数据规模 字典树用链表 #include <map> #include & ...

  8. HDU 11488 Hyper Prefix Sets (字符串-Trie树)

    H Hyper Prefix Sets Prefix goodness of a set string is length of longest common prefix*number of str ...

  9. 【HDU - 5790 】Prefix(主席树+Trie树)

    BUPT2017 wintertraining(15) #7C 题意 求[min((Z+L)%N,(Z+R)%N)+1,max((Z+L)%N,(Z+R)%N)+1]中不同前缀的个数,Z是上次询问的结 ...

随机推荐

  1. 解决 Eclipse “alt+/”快捷键 无效

    解决方案: 1. 检查windows ——preferences ——java ——editor —— content assist - advanced,在右上方有一行“select the pro ...

  2. chrome 插件

    SwitchySharp.crx SwitchyOmega.crx Readability_v3.0.15.crx Hackman.crx EditThisCookie_v1.4.1.crx AdBl ...

  3. 爱情之路(codevs 2070)

    题目描述 Description yh非常想念他的女朋友小y,于是他决定前往小y所在的那块大陆. 小y所在的大陆共有n个城市,m条双向路,每条路连接一个或两个城市.经过一条路ei需要耗费时间ti.此外 ...

  4. date +%s 能打印出自1970-01-01 00:00:00到当前时间的秒数

    [root@bass Desktop]# date +%s 1466561580 [root@bass Desktop]# python Python 2.6.6 (r266:84292, Jul 2 ...

  5. php设置和获取cookie

    php设置和获取cookie setcookie()调用只带有name参数的setcookie(); ()使失效时间为time()或time-; <?php setcookie(); PHP提供 ...

  6. 谷歌开源项目Chromium的源码获取与项目构建(Win7+vs10/vs13)

    转自:http://blog.csdn.net/kuerjinjin/article/details/23563059 从12年那会儿开始获取源码和构建chromium项目都是按照那时候的官方要求用w ...

  7. Android下拉刷新完全解析,教你如何一分钟实现下拉刷新功能 (转)

    转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/9255575 最 近项目中需要用到ListView下拉刷新的功能,一开始想图省事,在 ...

  8. 使用AIDL远程调用服务中的方法

    AIDL:android interface define language(接口定义语言) 作用:方便远程调用其他服务中的方法 注意:安卓四大组件都要在清单文件注册 aidl创建图: AIDL的全称 ...

  9. Ajax介绍

    AJAX AJAX = Asynchronous JavaScript and XML(异步的 JavaScript 和 XML). AJAX 不是新的编程语言,而是一种使用现有标准的新方法. AJA ...

  10. 文件上传漏洞演示脚本之js验证

    文件上传漏洞演示脚本之js验证 0 0       716   关于文件上传漏洞,想必玩web安全的同学们都有接触,之前本站也发布过一篇文章介绍文件上传漏洞的各种绕过方法,但是只是有文档却没有演示代码 ...