POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词。
这道题有4种方法可以做:
1.map
2.字典树
3.快排+二分
4.hash表
参考博客:POJ2503两种解法:快速排序+二分查找与哈希表
思路1:可以使用map来做
代码:
#include<iostream>
#include<stdio.h>
#include<string>
#include<map>
using namespace std; map<string,bool>appear;//记录单词的存在
map<string,string>translate;//记录单词的翻译
int main(){
char s[40];
char english[20];
char foreign[20];
while(gets(s) && s[0]){
sscanf(s,"%s%s",english,foreign);
appear[foreign]=true;
translate[foreign]=english;
}
while(scanf("%s",s)!=EOF){//vc6.0中需要按两下ctrl+z才能结束循环
if(appear[s]) cout<<translate[s]<<endl;//string 不能用printf输出
else printf("eh\n");
}
return 0;
}
vc6.0中需要按两下ctrl+z才能结束循环?详见:EOF的使用
思路2:用字典树来做
代码:
#include<iostream>
#include<cstring>
#include<stdio.h>
using namespace std; const int MAX=26;
struct Trie {
Trie *next[MAX];
int v; //根据需要变化,1代表无此单词,-1代表有此单词
char english[20];
};
Trie *root=new Trie; char s[40];
char english[20];
char foreign[20];
char out[20];//存储要输出的内容 void createTrie(char *str){
int len = strlen(str);
Trie *p = root, *q;
for(int i=0; i<len; ++i){
int id = str[i]-'a';
if(p->next[id] == NULL){
// q = (Trie *)malloc(sizeof(Trie));
q = new Trie;
q->v = 1; //初始v==1
for(int j=0; j<MAX; ++j)
q->next[j] = NULL;
p->next[id] = q;
}
p = p->next[id];
}
p->v = -1; //若为结尾,则将v改成-1表示
strcpy(p->english,english);
}
int findTrie(char *str){
int len = strlen(str);
Trie *p = root;
for(int i=0; i<len; ++i){
int id = str[i]-'a';
p = p->next[id];
if(p == NULL) //若为空集,表示不存以此为前缀的串
return 0;
}
if(p->v == -1){
strcpy(out,p->english);
return 1; //存在单词
}
return 0;
} int main(){
int i;
for(i=0;i<MAX;i++)
root->next[i]=NULL; while(gets(s) && s[0]){
sscanf(s,"%s%s",english,foreign);
createTrie(foreign);
}
while(scanf("%s",s)!=EOF){//vc6.0中需要按两下ctrl+z才能结束循环 if(findTrie(s)==1) printf("%s\n",out);
else printf("eh\n");
}
return 0;
}
思路3:快排+二分
代码:
思路4:hash表
代码:
POJ 2503 Babelfish(map,字典树,快排+二分,hash)的更多相关文章
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- poj 2503 Babelfish(字典树哈希)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 29059 Accepted: 12565 Description You hav ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- Poj 2503 Babelfish(Map操作)
一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...
- poj 2503 Babelfish(字典树或map或哈希或排序二分)
输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...
- POJ 2001 Shortest Prefixes(字典树)
题目地址:POJ 2001 考察的字典树,利用的是建树时将每个点仅仅要走过就累加.最后从根节点開始遍历,当遍历到仅仅有1次走过的时候,就说明这个地方是最短的独立前缀.然后记录下长度,输出就可以. 代码 ...
- poj 2503 哈希 Map 字典树
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 36967 Accepted: 15749 Descr ...
- poj 2503 Babelfish(字典树或着STL)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 35828 Accepted: 15320 Descr ...
- POJ 1002 487-3279(字典树/map映射)
487-3279 Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 309257 Accepted: 5 ...
随机推荐
- apache支持php
#tarzxvf php-5.2.9.tar.gz #cdphp-5.2.9 #./configure--prefix=/usr/local/php --with-apxs2=/usr/local/a ...
- UML的基本图(三)
An artifact diagram shows the physical constituents of a system on the computer. Artifacts includ ...
- php设计模式中的类型安全 指--只接受特定的对象 ---以避免发生错误
在百度百科中---类型安全代码指访问被授权可以访问的内存位置
- android 怎样加速./mk snod打包
mm命令高速编译一个模块之后,一般用adb push到手机看效果,假设环境不同意用adb push或模块不常常改.希望直接放到image里,则能够用./mk snod,这个命令只将system文件夹打 ...
- Visual Studio 2017 for Mac Preview
Microsoft Visual Studio 2017 for Mac Preview 下载+安装+案例Demo 目录: 0. 前言 1. 在线安装器 2. 安装VS 3. HelloWorld 4 ...
- 【转】cmd chcp命令切换字符格式
cmd chcp命令切换字符格式 命令介绍: chcp 65001 #换成utf-8代码页 chcp 936 #换成默认的gbk chcp 437 #美国英 ...
- EasyNVR RTSP转RTMP-HLS流媒体服务器前端构建之:内部搜索功能的实现
上一篇介绍了处理接口获取的数据,本篇将介绍如何在接收到的数据中搜索出自己符合条件的数据: 为了页面的美观,我们往往会以分页的形式来进行数据的展示.但是,当需要展示出来的数据太多的时候,我们很难迅速的找 ...
- 九度OJ 1036:Old Bill (老比尔) (基础题)
时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:2691 解决:1432 题目描述: Among grandfather's papers a bill was found. 72 ...
- python中的特殊用法
1 别名 from xxx import xxx as xxx;
- CNN延拓至 复数域