POJ_2503_Babelfish_(Trie/map)
描述
http://poj.org/problem?id=2503
给出一个字典,求翻译,翻译不了输出eh.
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 39335 | Accepted: 16797 |
Description
Input
Output
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
Source
分析
网上看到map可直接做,但我就是想打打Trie模板...
p.s.据说哈希也能做,但我完全不知道那是啥...
Trie做法:在每个单词节点存下对应翻译的字符串.
Trie:
#include <cstdio>
#include <iostream>
#include <cstring>
using namespace std; const int type=;
struct Trie{
struct node{
node* next[type];
bool v;
char word[];
node(){
v=false;
for(int i=;i<type;i++) next[i]=NULL;
for(int i=;i<;i++) word[i]='\0';
}
}*root;
Trie(){ root=new node; }
void insert(char *c1,char *c2){
node *o=root;
while(*c2){
int t=*c2-'a';
if(o->next[t]==NULL) o->next[t]=new node;
o=o->next[t];
c2++;
}
o->v=true;
strcpy(o->word,c1);
}
void query(char *c){
node* o=root;
while(*c){
int t=*c-'a';
if(o->next[t]==NULL){
printf("eh\n");
return;
}
o=o->next[t];
c++;
}
if(o->v) printf("%s\n",o->word);
else printf("eh\n");
}
}tree; int main(){
char c[],a[],b[];
while(cin.getline(c,)){
if(c[]=='\0') break;
sscanf(c,"%s %s",a,b);
tree.insert(a,b);
}
while(cin.getline(c,)){
if(c[]=='\0') break;
tree.query(c);
}
return ;
} Trie
map:
#include <iostream>
#include <cstdio>
#include <string>
#include <map>
using namespace std; char c[],a[],b[];
map <string,string> m; int main(){
while(cin.getline(c,)){
if(c[]=='\0') break;
sscanf(c,"%s %s",a,b);
m[b]=a;
}
map <string,string> :: iterator it;
while(cin.getline(c,)){
if(c[]=='\0') break;
it=m.find(c);
if(it!=m.end()){
printf("%s\n",it->second.c_str());
}
else{
printf("eh\n");
}
}
return ;
} map
POJ_2503_Babelfish_(Trie/map)的更多相关文章
- BZOJ 2754 [SCOI2012]喵星球上的点名 (AC自动机+map维护Trie树)
题目大意:略 由于字符集大,要用map维护Trie树 并不能用AC自动机的Trie图优化,不然内存会炸 所以我用AC自动机暴跳fail水过的 显然根据喵星人建AC自动机是不行的,所以要根据问题建 然而 ...
- 数据结构 - trie
#include <cstring> #include <iostream> #include <map> #include <cstdio> usin ...
- trie字典树模板浅析
什么是trie? 百度百科 又称单词查找树,Trie树,是一种树形结构,是一种哈希树的变种.典型应用是用于统计,排序和保存大量的字符串(但不仅限于字符串),所以经常被搜索引擎系统用于文本词频统计.它的 ...
- 布隆过滤器(Bloom Filter)的原理和实现
什么情况下需要布隆过滤器? 先来看几个比较常见的例子 字处理软件中,需要检查一个英语单词是否拼写正确 在 FBI,一个嫌疑人的名字是否已经在嫌疑名单上 在网络爬虫里,一个网址是否被访问过 yahoo, ...
- Bloom Filter布隆过滤器原理和实现(1)
引子 <数学之美>介绍布隆过滤器非常经典: 在日常生活中,包括设计计算机软件时,经常要判断一个元素是否在一个集合中.比如: 在字处理软件中,需要检查一个英语单词是否拼写正确(也就是要判断它 ...
- [GitHub] 75+的 C# 数据结构和算法实现
C#中标准数据结构和算法的即插即用类库项目 GitHub:https://github.com/aalhour/C-Sharp-Algorithms Watch: 307 Star: 3.4k For ...
- [ACM] POJ 2418 Hardwood Species (Trie树或map)
Hardwood Species Time Limit: 10000MS Memory Limit: 65536K Total Submissions: 17986 Accepted: 713 ...
- 【一题多解】 map 二分 hash trie poj 2503
各种方式解这道题!! 利用map 超时了 #include <iostream> #include <string> #include <map> using na ...
- hdu 1251:统计难题[【trie树】||【map】
<题目链接> 统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131 ...
随机推荐
- Hyper-V Windows 8.1 & Windows Server 2012 R2 Q&A
从Windows8开始,x64位系统自带Hyper-V功能,很多开发者和专业用户往往希望利用的Microsoft提供的这一免费功能,但是微软在这方面并不是最佳. 主要写几个大家经常遇到的问题. Win ...
- Eclipse 配置SSH 详解
http://blog.csdn.net/binyao02123202/article/details/18446523 最近看了很多招聘,其中很多我想去的公司都需要一些技能,其中熟练 Java SS ...
- HTML标签_Form
理解HTML是如何跳转到java程序中去的:Form常用HTML标签的作用<body> <form action="servlet/UploadServlet" ...
- spring mvc 笔记
springmvc 课堂笔记 1.Springmvc是什么 Spring Web MVC是一种基于Java的实现了Web MVC设计模式的请求驱动类型的轻量级Web框架,即使用了MVC架构模式的思想, ...
- IDC机房动力环境设备维护
高低压配电 空调 ...
- TFTPD32, 3CDaemon, FlashFxp
TFTPD32, 3CDaemon, FlashFxp ——各种网络传输下载工具简介—— 一.将3CDaemon.exe作为TFTP服务端,开发板作为TFTP客户端 1.如上图所示,设置好3CDaem ...
- php安装过程中遇到的需要安装的问题
http://www.cnblogs.com/kristain/articles/3809243.html 借鉴php安装错误 2013-01-04 19:16:49 分类: 系统运维 环境: ...
- 将数据库二进制图片导出显示到EPPlus Excel2007中
1.EPPlus Excel 控件可以参考我的另一篇博客:http://blog.163.com/pei_huiping/blog/static/206573067201281810549984/ 这 ...
- HTML5格式化
<!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content ...
- C语言-05内存剖析
1.进制 1. 二进制 1> 特点:只有0和1,逢2进1 2> 书写格式:0b或者0b开头 3> 使用场合:二进制指令\二进制文件,变量在内存中就是二进制存储 ...