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 ...
随机推荐
- 使用python在WEB页面上生成EXCEL文件
来自:http://blog.sina.com.cn/s/blog_5d18f85f0101bxo7.html 近日写的一个程序需要在WEB服务器上生成EXCEL文件供用户下载,研究了一下找到了以下比 ...
- 03-spring学习-自动装配
自动装配 新建person类: package com.spring.bean.autowire; public class Person { private String name; private ...
- C#调用 oracle存储过程
C#调用oracle 存储过程与调用Sql server存储过程类似,比较简单:直接给出示例: /// <summary> /// 判断物料类型是不是总部管控 /// </summa ...
- unity3d贴图2D
在GUI上绘制图片步骤如下: 1.定义一个2D图片纹理变量: public Texture2D pic; 2.关联变量和贴图的关系: 在布局界面选中MainCamera,找到右侧属性列表中的pic选项 ...
- js操作时间 加法 减法 计算 格式化时间
Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...
- centos终端显示-bash-4.2#解决方法
登录linux系统过后,发现显示的是-bash-4.2# 而不是root@主机名 + 路径的显示方式,发生这种情况的原因是根目录下缺失几个配置文件,从默认配置中拷贝过来就可以解决了: cp /etc/ ...
- 论Top与ROW_NUMBER读取第一页的效率问题及拼接sql查询条件
http://www.cnblogs.com/Leo_wl/p/4921799.html SELECT TOP * FROM users WHERE nID> And nID< ORDER ...
- poj1860--Currency Exchange
Bellman-ford算法的反向应用--正循环检查 /** \brief poj 1860 Bellman-Ford * * \param date 2014/7/24 * \param state ...
- 多线程-CAS原理
背景 在JDK1.5之前Java语言是靠synchronized关键字保证同步的,这会导致有锁,锁机制存在以下问题: (1)在多线程竞争中,加锁.释放锁会导致比较多的上下文切换和调度延时,引起性能问题 ...
- 调试JDK1.8源码的方法
背景 在学习JDK源码的时候,免不了需要调试JDK的源码. 比如:想理解ConcurrentHashMap的put(K k, V v)方法,JDK自带的rt.jar文件是支持断点调试,但是却看不到变量 ...