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 ...
随机推荐
- MySQL数据库如何导入导出
1 点击任意一个数据库,然后点击导出,导出为SQL格式,其他一切保持默认(不要勾选"添加 DROP TABLE/DROP VIEW") 2 勾选"另存为文件"点 ...
- 搭建Vue环境总是出错,就重新安装就好了
总是报错,还不如重新安装.. 错误千奇百怪,解决了 这个错误又会出现另外一个. 百度了一个挺好用的e 在window下搭建Vue.Js开发环境 nodejs官网http://nodejs.cn/下 ...
- 嵌入web字体
@font-face模块 可以帮助我们轻松解决Web页面中使用优雅字体的方式,而且我们可以根据需要自定义多种字体,但是每个@font-face只能定义一种,若需要多个字体就启用多个@f ...
- 【VBA编程】13.Workbook对象的事件
Workbook事件用于响应对Workbook对象所进行的操作. [BeforeClose事件] BforeClose事件用于响应窗口关闭的操作 在工程资源器中,双击“ThisWorkbook”对象, ...
- centos7 install flash player
1.在 https://get.adobe.com/cn/flashplayer/ 上选择需要下载版本---> ( YUM,适用于Linux (YUM) ); 2.进入root权限后,进入你的下 ...
- expect脚本免密码
#!/usr/bin/expect set timeout spawn ssh root@20.0.102.19 expect "password:" send "123 ...
- X86服务器、小型机、大型机、塔式、机架式、刀片式服务器、工作站
ü 服务器分:x86(PC)服务器,小型机(Unix服务器),大型机: pc服务器则主要指基于intel处理器的x86架构,是一个通用开放的系统. UNIX服务器,也就是中国业内习惯上说的小型机,在 ...
- Android Shell Cmd
1. view android version: grep ro.build.version.sdk= system/build.prop getprop ro.build.version.relea ...
- Mac上的学习神器:Marginnote
https://marginnote.com/?lang=zh-hans 技巧1:合并 多选编辑 - 按顺序选择多个块 - 左下角菜单 - 合并 技巧2:管理顺序 双击图片并且按住不放,即可拖拽顺序 ...
- PsExec使用
01. 创建一个 Process Process.FileName ="文件路径及文件名称" Process.Arguments ="\\PC PI地址 -u 用户名 - ...