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 (Java/Others)
Total Submission(s): 16042 Accepted Submission(s): 5198
into English. Can you help him?
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.
START
from fiwo
hello difh
mars riwosf
earth fnnvk
like fiiwj
END
START
difh, i'm fiwo riwosf.
i fiiwj fnnvk!
END
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)的更多相关文章
- 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 ...
- 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 ...
- hdu 1075 What Are You Talking About(map)
题意:单词翻译 思路:map #include<iostream> #include<stdio.h> #include<string.h> #include< ...
- 【python】Leetcode每日一题-前缀树(Trie)
[python]Leetcode每日一题-前缀树(Trie) [题目描述] Trie(发音类似 "try")或者说 前缀树 是一种树形数据结构,用于高效地存储和检索字符串数据集中的 ...
- 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 ...
- HDU 1087:Super Jumping! Jumping! Jumping!(LIS)
Super Jumping! Jumping! Jumping! Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- HDU 1671 Phone List (Trie)
pid=1671">Phone List Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 1358 Period (kmp求循环节)(经典)
<题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...
随机推荐
- http://blog.csdn.net/zxl315/article/details/10830105
http://blog.csdn.net/zxl315/article/details/10830105
- [Python爬虫] 之三十:Selenium +phantomjs 利用 pyquery抓取栏目
一.介绍 本例子用Selenium +phantomjs爬取栏目(http://tv.cctv.com/lm/)的信息 二.网站信息 三.数据抓取 首先抓取所有要抓取网页链接,共39页,保存到数据库里 ...
- R简易安装
post={"title":"my Blog post","content":"Here's my blog post" ...
- 解决SSH窗口关闭,linux上的应用也关闭
最近在应用linux上的服务的时候发现一个问题 使用SSH远程连接启动的应用在SSH关闭的时候也死掉了,网上查了一下原因 大致是说SSH在关闭的时候会发送一个终止的指令给应用,然后就停了 简要的解决办 ...
- git remote branch操作
将本地branch basic提交到remote的basic上: git push origin basic:basic 将remote的 basic branch更新到本地的 basic branc ...
- Linux远程执行Windows机器任务
Linux远程执行Windows机器任务 近期测试人员提出需求需要在Linux下调用Windows系统下的cmd的命令完成自动构建和测试并生成测试报告. 环境: Windows Server2 ...
- (转)nio 连网和异步 I/O
连网和异步 I/O 概述 连网是学习异步 I/O 的很好基础,而异步 I/O 对于在 Java 语言中执行任何输入/输出过程的人来说,无疑都是必须具备的知识.NIO 中的连网与 NIO 中的其他任何操 ...
- Win7无法安装Flash Player怎么办
在IE的工具选项中把安全选项中的ActiveX控件一系列都改为启用即可.
- android wifi调试(无线调试) 一步到位
没有数据线时候,怎么进行调试开发?只要在一个局域网内,最好选择wifi调试! 网上有很多这样的教程,但是有很多步.很繁琐.最近我在gp上下载了一个软件可以实现点击一步就可以了.不需要在手机上输入任何命 ...
- java中==与equal()的区别
==和equal()都是用来判断两个变量是否相等的. (1)如果两个变量是基本类型变量,且都是数值型的(不一定数据类型相同),只要是值相同,将返回true; (2)如果两个变量是引用型变量,只有它们指 ...