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组成: 第 ...
随机推荐
- 流畅的python第十一章接口学习记录
鸭子协议(忽略对象真正类型,转而关注对象有没有实现所需的方法,签名和语义) 标准库中的抽象基类 collections.abc模块中的抽象基类 抽象方法是抽象基类中用来强制子类必须实现的方法,如果子类 ...
- metal的gpu query
https://developer.apple.com/documentation/metal/mtlcommandbuffer/1639924-gpustarttime gpuStartTime 看 ...
- Hadoop数据目录迁移
Hadoop数据目录迁移 @(Hadoop) 随着数据的不断导入和增大,原本集群部署的目录磁盘空间不足了,所以要把hadoop存储数据的位置迁移到另外一个巨大的磁盘上,另外的一个用意是将数据和程序分离 ...
- 【笔记】js Array.prototype.slice.call(arguments) 将函数的参数转换为数组方法的见解
我们知道函数里面的参数实际上是一个以数组形式储存的对象 但它并非一个数组 如果我们要将它转换为数组可以调用Array.prototype.slice() 这个方法 分析一下这个方法: Array.pr ...
- C#秘密武器之委托
在C#的世界里,委托是无处不在,尤其在.NET自己封装的框架里到处都有其身影,所以掌握委托就很有必要了!那什么是委托呢?其实委托就是一种数据类型,跟int等东东差不多,只不过在使用时要自己先去构建一个 ...
- Hello Socket - 第一个Socket程序
1. 首先,要编写windows下socket程序,必须要加入Winsock支持 2. 服务端监听程序(Server.cpp) #include<winsock2.h> //包含头文件 # ...
- tornado ThreadPoolExecutor
import os import sys import time import tornado.httpserver import tornado.ioloop import tornado.opti ...
- linux Java 手动GC 手动回收垃圾
logs_paths[0]="xxxx_tomcat8_9001"; logs_paths[1]="xxxx_tomcat8_9002"; for logs_p ...
- vfork & fork
转载 http://coolshell.cn/articles/12103.html 在知乎上,有个人问了这样的一个问题——为什么vfork的子进程里用return,整个程序会挂掉,而且exit()不 ...
- Spring MVC坑汇总+Stackoverflow巧解答
1.http://stackoverflow.com/questions/25598406/spring-annotaion-autowired-inside-methods Q: Autowire ...