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. 编译 arm 版的qt

    因为项目需要,我们需要在开发板上使用QT开发平台,因此需要编译一个arm版的QT. 在网上找了一些资料,费了几天时间,终于成功了. 第一步,准备源码 先下载QT 源码,在http://qt-proje ...

  2. 解决ping 127.0.0.1不通的问题

    用树莓派放在家里当pt下载器,一直挺惬意的,因为没有公网ip用vps和frp配置代理,偶尔ssh上去看看,一段时间也用得好好的. 可是最近这几天,在办公室ssh上去死活连不上. 于是回去后开始折腾,局 ...

  3. Junit测试Spring应用Dubbo测试框架之-Excel 工具类

    package com.tree.autotest.demo; import com.alibaba.fastjson.JSON;import org.apache.poi.hssf.usermode ...

  4. 常用HTML标签的全称及描述

    常用HTML标签的英文全称及简单描述   HTML标签 英文全称 中文释义 a Anchor 锚 abbr Abbreviation 缩写词 acronym Acronym 取首字母的缩写词 addr ...

  5. 关于RTP中的时间戳问题

    关于RTP中的时间戳问题 时间戳单位:时间戳计算的单位不为秒之类的单位,而是由采样频率所代替的单位,这样做的目的就是为了是时间戳单位更为精准.比如说一个音频的采样频率为8000HZ,那么我们可以把时间 ...

  6. JMeter 三:搭建一个Web Test Plan

    参考:http://jmeter.apache.org/usermanual/build-web-test-plan.html 场景 5个用户并发测试百度搜索,有两个请求,一个请求搜索“你好”,一个请 ...

  7. selenium-Getting Started

    1.1. Simple Usage If you have installed Selenium Python bindings, you can start using it from Python ...

  8. Struct2-使用随笔

    提要:最近写了一个2-3各页面的使用Struts2的Demo,在这里做下总结,都已经3年多没做J2EE了,有些感觉都快找不到了. 目录 1.必备开发工具 2.必备jar包 3.了解知识点 4.遇到的问 ...

  9. Hibernate 一对一关联查询

    版权声明:本文为博主原创文章,如需转载请标注转载地址. 博客地址:http://www.cnblogs.com/caoyc/p/5602418.html  一对一关联,可以分为两种.一种是基于外键的关 ...

  10. JDBC 使用SimpleJdbcTemplate实现Dao

    public interface UserDao {     public void addUser(User user);     public User getUser(int userId); ...