poj 2503 Babelfish(字典树或着STL)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 35828 | Accepted: 15320 |
Description
Input
more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.
Output
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
Source
field=source&key=Waterloo+local+2001.09.22" style="text-decoration:none">Waterloo local 2001.09.22
STL相同秒杀 我就怀疑字典树有个鸡毛用,測试例子太少了吧!
STL:
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std; int main()
{
string str;map<string,string >cnt;
while(getline(cin,str)&&str[0]!=0)
{
int loc=str.find(" ");
cnt[str.substr(loc+1,str.size()-loc-1)]=str.substr(0,loc);
}
while(cin>>str)
{
if(cnt.count(str))
cout<<cnt[str]<<endl;
else
cout<<"eh"<<endl;
}
return 0;
}
字典树:
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef struct NodeType
{
struct NodeType * child[26];
char word [11];
int isWord;
NodeType()
{
memset(child,NULL,sizeof(child));
isWord=0;
}
} Node;
void insertWord(Node *node,char *wo,char *wt)
{
int id;
while(*wt)
{
id=*wt-'a';
if(node->child[id]==NULL)
node->child[id]=new Node();
node=node->child[id];
wt++;
}
node->isWord=1;
strcpy(node->word,wo);
}
char * searchWord(Node * node,char *wd)
{
char *noWord="eh";
int id;
while(*wd)
{
id=*wd-'a';
if(node->child[id]==NULL)
return noWord;
node=node->child[id];
wd++;
}
if(node->isWord)
return node->word;
else
return noWord;
}
int main()
{
char cnt[40],dict[40],buf[40];
Node * node=new Node;
while(gets(buf)&&buf[0]!=0)
{
int i=0;
for(; buf[i]!=32; i++)
cnt[i]=buf[i];
cnt[i]=0;
int j;
for(++i,j=0; buf[i]; i++,j++)
dict[j]=buf[i];
dict[j]=0;
insertWord(node,cnt,dict);
}
while(scanf("%s",cnt)!=EOF)
{
char *word = searchWord(node,cnt);
printf("%s\n",word);
}
return 0;
}
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
typedef struct NodeType
{
struct NodeType * child[26];
char word [11];
int isWord;
NodeType()
{
memset(child,NULL,sizeof(child));
isWord=0;
}
} Node; void insertWord(Node *node,char *wo,char *wt)
{
int id;
while(*wt)
{
id=*wt-'a';
if(node->child[id]==NULL)
node->child[id]=new Node();
node=node->child[id];
wt++;
}
node->isWord=1;
strcpy(node->word,wo);
} char * searchWord(Node * node,char *wd)
{
char *noWord="eh";
int id;
while(*wd)
{
id=*wd-'a';
if(node->child[id]==NULL)
return noWord;
node=node->child[id];
wd++;
}
if(node->isWord)
return node->word;
else
return noWord;
} int main()
{
char cnt[40],dict[40],buf[40];
Node * node=new Node;
while(gets(buf)&&buf[0]!=0)
{
int i=0;
for(; buf[i]!=32; i++)
cnt[i]=buf[i];
cnt[i]=0;
int j;
for(++i,j=0; buf[i]; i++,j++)
dict[j]=buf[i];
dict[j]=0;
insertWord(node,cnt,dict);
}
while(scanf("%s",cnt)!=EOF)
{
char *word = searchWord(node,cnt);
printf("%s\n",word);
}
return 0;
}
poj 2503 Babelfish(字典树或着STL)的更多相关文章
- poj 2503 Babelfish(字典树或map或哈希或排序二分)
输入若干组对应关系,然后输入应该单词,输出对应的单词,如果没有对应的输出eh 此题的做法非常多,很多人用了字典树,还有有用hash的,也有用了排序加二分的(感觉这种方法时间效率最差了),这里我参考了M ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
- POJ 2503 Babelfish
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28766 Accepted: 12407 Descripti ...
- poj 2513(欧拉路径+字典树映射)
题目链接:http://poj.org/problem?id=2513 思路:题目还是很简单的,就是判断是否存在欧拉路径,我们给每个单词的头和尾映射序号,统计度数.对于给定的无向图,当且仅当图连通并且 ...
- POJ 1451 - T9 - [字典树]
题目链接:http://bailian.openjudge.cn/practice/1451/ 总时间限制: 1000ms 内存限制: 65536kB 描述 Background A while ag ...
- POJ 2513 【字典树】【欧拉回路】
题意: 有很多棒子,两端有颜色,告诉你两端的颜色,让你把这些棒子拼接起来要求相邻的接点的两个颜色是一样的. 问能否拼接成功. 思路: 将颜色看作节点,将棒子看作边,寻找欧拉通路. 保证图的连通性的时候 ...
- poj 2503 Babelfish(字典树哈希)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 29059 Accepted: 12565 Description You hav ...
随机推荐
- 制作mac U盘启动
之前在windows电脑上装系统,U盘,光盘都可以! 当然在mac电脑上也是可以的! 公司电脑mac mini 没有光驱,只有用U盘装了!折腾了一天,就是做不上10.9的U盘启动,最后发现是,10.9 ...
- Unity Mono foreach BUG性能测试
# 环境 - Unity 4.6.4 / Windows # 测试代码 # 结果数据 # 结论 foreach存在bug,会导致GC,并且效率低下: 使用GetEnumerator代替,没有GC,并且 ...
- 通过LR监控Linux服务器性能
1)设置监控服务器要监控 UNIX 资源,必须配置 rstatd 守护程序.注意,可能已经配置了 rstatd守护程序,因为当计算机收到一个 rstatd 请求时,该计算机上的 inetd 自动激活r ...
- Openerp开发进销存系统总结
转自 :http://blog.sina.com.cn/s/blog_7cb52fa80101ngt8.html 差不多用了2个星期的闲余事件,对于openerp v7.0进行了学习和应用开发.细节总 ...
- pandas数据处理基础——基础加减乘除的运算规则
上周公司对所有员工封闭培训了一个星期,期间没收手机,基本上博客的更新都停止了,尽管培训时间不长,但还是有些收获,不仅来自于培训讲师的,更多的是发现自己与别人的不足,一个优秀的人不仅仅是自己专业那块的精 ...
- cisco asa5510 配置
anyconnect 查看vpn链接 ASA版本8.4(7) anyconnect版本3.1 亲测sh vpn-sessiondb anyconnect 查看登录用户详情sh vpn-ses ...
- IIS6.0应用程序池回收(转载)
这段时间公司的程序经常出现问题,然后整个应用程序就不能访问了,我们的服务器版本:window 2003 SP1,IIS6.0,没有安装Microsoft Visual Studio .NET . 问题 ...
- ubuntu系统安装nginx出现的错误(依赖环境没有安装完)
报错信息: error: the HTTP image filter module requires the GD library. 编译参数:(或源安装) ./configure --prefix= ...
- SELinux 宽容模式(permissive) 强制模式(enforcing) 关闭(disabled)
SElinux共有3中状态.1.selinux的配置文件:/etc/selinux/config# This file controls the state of SELinux on the sys ...
- 进程隐藏与进程保护(SSDT Hook 实现)(三)
文章目录: 1. 引子: 2. 获取当前系统下所有进程: 3. 服务管理(安装,启动,停止,卸载): 4. 应用程序和内核程序通信: 5. 小结: 1. 引子: 关于这个 SSDT Hook 实现进程 ...