POJ 2503
http://poj.org/problem?id=2503
题意就是字典翻译。这个输入输出真心恶心,要不是看discuss我肯定是解决不了,还用上了sscanf函数。。。。
这道题我用几种方法做。
sscanf与scanf类似,都是用于输入的,只是后者以屏幕(stdin)为输入源,前者以固定字符串为输入源。
一 qsort+二分
//Memory:5060K c++run time:422MS
#include <stdio.h>
#include <iostream>
#include <string>
#include <string.h>
#include <stdlib.h> using namespace std; struct sm{
char dic[];
char eng[];
}str[]; char st[],a[]; int cmp(const void *a,const void *b)
{
return strcmp((*(sm*)a).dic,(*(sm*)b).dic);
} int main()
{
int n=;
while(gets(a)){
if(a[] == '\0') break;
sscanf(a,"%s%s",str[n].eng,str[n].dic);
n++;
}
qsort(str,n,sizeof(str[]),cmp);
while(gets(st)&&st[]!='\0')
{
int low=,high=n,flag=,mid;
while(low<=high)
{
mid=(low+high)/;
if(!strcmp(st,str[mid].dic))
{
flag=;
printf("%s\n",str[mid].eng);
break;
}
else
if(strcmp(st,str[mid].dic)>)
low=mid+;
else
if(strcmp(st,str[mid].dic)<)
high=mid-;
}
if(flag==) printf("eh\n");
}
return ;
}
二、用map
这是我第一次用map这个函数,这也只是比较水,当然,这个题也比较水,map有个好处就是他会对里面所映射的元素进行排序
其中count是查找这个元素是否存在这个Map中
Memery:9624K c++ runtime:688MS
#include <stdio.h>
#include <iostream>
#include <map>
#include <string> using namespace std; map<string,string>mp; int main()
{
char a[],b[],c[];
while(gets(a)&&a[]!='\0')
{
sscanf(a,"%s%s",b,c);
mp[c]=b;
}
while(gets(a)&&a[]!='\0')
{
if(!mp.count(a)) cout<<"eh"<<endl;
else cout<<mp[a]<<endl;
}
return ;
}
三、Hash
POJ 2503的更多相关文章
- poj 2503(字符串)
http://poj.org/problem?id=2503 题意:就是翻译,给你一个字典,然后再查找单词,找得到的就输出单词,找不到的输出eh,用Map水题一个,但这个题有点意思的就是输入的问题 # ...
- POJ 2503 字典树
题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2] 意思是string2的值为string1. 然后给定一波 ...
- Poj 2503 / OpenJudge 2503 Babelfish
1.Link: http://poj.org/problem?id=2503 http://bailian.openjudge.cn/practice/2503/ 2.Content: Babelfi ...
- poj 2503 Babelfish (查找 map)
题目:http://poj.org/problem?id=2503 不知道为什么 poj 的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...
- poj 2503 字符串hash
题目链接:http://poj.org/problem?id=2503 代码: #include<cstdio> #include<cstring> #include<i ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- 【POJ 2503】 Babelfish
[题目链接] http://poj.org/problem?id=2503 [算法] 字符串哈希 [代码] #include <algorithm> #include <bitset ...
- 【POJ 2503】Babelfish(字符串)
题 给定字典,再询问. 字典与询问之间有一个空行. cin.peek()是一个指针指向当前字符. #include<iostream> #include<string> #in ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
随机推荐
- 工具,百度编辑器 UEditor 使用实例化
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- Jetty多Connector
有时候想要启动两个端口,或者通过一个Jetty server提供多个不同服务,比如说使用8080来指定默认访问端口,使用8433指定https访问端口等等,此时就可以通过创建多个Connector来解 ...
- 用HTML/JS/PHP方式实现页面延时跳转
WEB开发中经常会遇到页面跳转或延时跳转的需求,掌握各种页面跳转方式非常必要. 以下是我总结有用HTML/JS/PHP三类方式实现跳转的方法,例子皆为三秒后跳转到index.php 页面. 1,HTM ...
- 用jquery写循环播放div -2
前面所说的class html元素标签的写法也要有层次性, 这个层次性其实也就是常说的 css类写法要有一个"命名空间, 名字空间" "namespace" 在 ...
- 如何解压.bz2文件包
.bz2 解压1:bzip2 -d FileName.bz2 解压2:bunzip2 FileName.bz2 压缩: bzip2 -z FileName .tar.bz2 解压:tar j ...
- 更改vs自带的模板
路径:C:\Program Files (x86)\Microsoft Visual Studio 12.0\Common7\IDE\ItemTemplatesCache\CSharp\Code\20 ...
- IOS开发之----NSDictionary,JSON和XML互相转换
本文永久地址为 http://www.cnblogs.com/ChenYilong/p/4044521.html,转载请注明出处. -(void)test { //XML文本范例 ...
- 必须知道的.net(字段、属性和方法)
1.字段 通常定义为private(封装原则) 2.属性(property) 通常定义为public,表示类的对外成员.具有可读可写属性,通过get和set访问器实现 3.索引器(indexer) C ...
- oracle删除表以及清理表空间
若要彻底删除表,则使用语句:drop table <table_name> purge; 清除回收站里的信息 清除指定表:purge table <table_name>; 清 ...
- 【知乎网】Linux IO 多路复用 是什么意思?
提问一: Linux IO多路复用有 epoll, poll, select,知道epoll性能比其他几者要好.也在网上查了一下这几者的区别,表示没有弄明白. IO多路复用是什么意思,在实际的应用中是 ...