What Are You Talking About

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

Total Submission(s): 16042    Accepted Submission(s): 5198

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,相应的相应单词作为原始单词的附加信息放到Trie树中原始单词的最后一个字符结点中。

AC代码:

//#include <bits/stdc++.h>       //hdu的C++,不支持此头文件。G++支持
#include <cstdio>
#include <cstring>
#include <cctype> using namespace std; const int maxw = 12;
const int maxnode = 100000 * maxw + 10;
const int sigma_size = 26; struct Trie{
int ch[maxnode][sigma_size];
char val[maxnode][maxw];
int sz; void clear(){ sz = 1; memset(ch[0], 0, sizeof(ch[0])); }
int idx(char c){ return c - 'a'; } void insert(const char *s, const char *v){
int u = 0, n = strlen(s);
for(int i=0; i<n; i++){
int c = idx(s[i]);
if(!ch[u][c]){
memset(ch[sz], 0, sizeof(ch[sz]));
ch[u][c] = sz++;
}
u = ch[u][c];
}
strcpy(val[u], v);
} char* find(const char *s){
int u = 0, n = strlen(s);
int i;
char ans[maxw];
for(i=0; i<n; i++){
int c = idx(s[i]);
if(!ch[u][c]) return "";
u = ch[u][c];
}
return val[u];
}
}; Trie trie; int main(){
#ifdef sxk
freopen("in.txt", "r", stdin);
#endif // sxk trie.clear();
char s[maxw], ss[maxw], text[3002];
scanf("%s", s);
while(scanf("%s", s)){
if(s[0] == 'E' && s[1] == 'N' && s[2] == 'D') break;
scanf("%s", ss);
trie.insert(ss, s);
}
scanf("%s", s);
getchar();
while(gets(text)){
if(text[0] == 'E' && text[1] == 'N' && text[2] == 'D') break;
int n = strlen(text);
for(int i=0; i<n; ){
char foo[maxw];
int cnt = 0;
while(i < n && isalpha(text[i])){ foo[cnt ++] = text[i ++]; }
foo[cnt] = '\0';
if(!cnt){ putchar(text[i ++]); continue; }
if(strcmp(trie.find(foo), "")) printf("%s", trie.find(foo));
else printf("%s", foo);
}
puts("");
}
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 (strings)

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

  3. hdu 1075 What Are You Talking About(map)

    题意:单词翻译 思路:map #include<iostream> #include<stdio.h> #include<string.h> #include< ...

  4. 【python】Leetcode每日一题-前缀树(Trie)

    [python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...

  5. HDU 1052 Tian Ji -- The Horse Racing(贪心)(2004 Asia Regional Shanghai)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1052 Problem Description Here is a famous story in Ch ...

  6. HDU 1087:Super Jumping! Jumping! Jumping!(LIS)

    Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 ...

  7. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  8. HDU 1671 Phone List (Trie)

    pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  9. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

随机推荐

  1. RMAN备份与恢复之概念一

    1.  数据库完全备份: 按归档模式分为归档和非归档 归档模式 打开状态,属于非一致性备份 关闭状态,可以分为一致性和非一致性 非归档模式 打开状态,非一致性备份无效 关闭状态,一致性备份,非一致性备 ...

  2. ORA-01219:数据库未打开:仅允许在固定表/视图中查询

    好久没有登陆到Oracle的服务器了,把密码都忘记了.sql>conn sys/sys as sysdba;sql>alter user system identified by *;结果 ...

  3. 编译安装mysql-5.6.40

    编译安装mysql-5.6.40 环境说明 系统版本     CentOS 7.2 x86_64 软件版本     mysql-5.6.40 [root@db01 ~]# mkdir -p /serv ...

  4. 病毒木马查杀实战第015篇:U盘病毒之脱壳研究

    前言 因为我们的终于目标是编写出针对于这次的U盘病毒的专杀工具.而通过上次的分析我们知道,病毒有可能在不同的计算机中会以不同的名称进行显示.假设真是如此,那么就有必要在此分析出病毒的命名规律等特征,然 ...

  5. 主动通知Android系统图库进行更新

    项目中遇到调用图库进行图片的选择,因为不能主动及时更新,遂实现代码调用实现主动及时更新. 废话不多刷,看代码. 方式一,发送一个广播, sendBroadcast(new Intent(Intent. ...

  6. 直接插入排序、折半插入排序、shell插入排序

    直接插入排序:   折半插入排序:   shell插入排序:  

  7. iOS 使用 AVCaptureVideoDataOutputSampleBufferDelegate获取实时拍照的视频流

    iOS 使用 AVCaptureVideoDataOutputSampleBufferDelegate获取实时拍照的视频流 可用于实时视频聊天 实时视频远程监控 #import <AVFound ...

  8. 如何利用ps去批量完成一套任务

    作为前端开发人员,不说设计你会不会,ps作为一个工具来说,前端开发人员还是要熟悉才行的 做了一个项目,客户自己上传了图片,他表示上传非常慢,我们表示不解,为何那么慢,网络问题吗,经过看了她的图片,发现 ...

  9. 算法笔记_165:算法提高 道路和航路(Java)

    目录 1 问题描述 2解决方案   1 问题描述 问题描述 农夫约翰正在针对一个新区域的牛奶配送合同进行研究.他打算分发牛奶到T个城镇(标号为1..T),这些城镇通过R条标号为(1..R)的道路和P条 ...

  10. ionic 图片加载失败,显示默认图片代替

    1.首先编写自定义指令 angular.module('starter.directives', []) //当图片找不到事显示替代图片 .directive("errSrc", ...