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 ...
随机推荐
- ASP.NET Padding Oracle Attack EXP
#!/usr/bin/perl## PadBuster v0.3 - Automated script for performing Padding Oracle attacks# Brian Hol ...
- 百度文件上传webuploader上传文件,含文件大小、类型验证
你的上传初始化文件upload_XXX.js中:(见红色字) // 初始化Web Uploader var allMaxSize = 10; var uploader = WebUploader.cr ...
- Yii2 redis与cache
原文地址:http://www.myexception.cn/php/1974979.html composer require yiisoft/yii2-redis 安装后使用超简单,打开 comm ...
- linux下文件结束符
linux下文件结束符,我试过了所有的linux,发现其文件的结束符都是以0a即LF结束的,这个是操作系统规定的,windows下是\r\n符结束,希望可以帮助大家. -------------转:来 ...
- fedora 23如何实现 让root用户自动登录?
没想到很简单: 只是修改一个文件的一个地方: 修改: /etc/gdm/custom.conf文件, 将自动登录 启用为true, 然后自动登录的名字设为root 即可:
- 优化PHP程序的方法(温故知新)
1. If a method c++an be static, declare it static. Speed improvement is by a factor of 4. 如果一个方法可静态化 ...
- Redhat系统网络配置
1.RedHat系统的网络配置文件/etc/sysconfig/network-scirpts/ifcfg-<interface-name>文件 DEVICE=eth0 ...
- LYDSY模拟赛day3 涂色游戏
/* 非常好的题 */ #include <cstdio> #include <iostream> #include <cstdlib> #include < ...
- BeanFactory和ApplicationContext的区别
1.BeanFactory和ApplicationContext的异同点: 相同点: 两者都是通过xml配置文件加载bean,ApplicationContext和BeanFacotry相比 ...
- 解决访问ajax.googleapis.com链接失败方法
参考文章:http://www.jianshu.com/p/690e28f7fde6 主要思路:修改hosts文件,将其网址ajax.googleapis.com指向本地服务器:本地服务器将通过aja ...