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的更多相关文章

  1. poj 2503(字符串)

    http://poj.org/problem?id=2503 题意:就是翻译,给你一个字典,然后再查找单词,找得到的就输出单词,找不到的输出eh,用Map水题一个,但这个题有点意思的就是输入的问题 # ...

  2. POJ 2503 字典树

    题目链接:http://poj.org/problem?id=2503 题意:给定一个词典,输入格式为[string1' 'string2]  意思是string2的值为string1. 然后给定一波 ...

  3. Poj 2503 / OpenJudge 2503 Babelfish

    1.Link: http://poj.org/problem?id=2503 http://bailian.openjudge.cn/practice/2503/ 2.Content: Babelfi ...

  4. poj 2503 Babelfish (查找 map)

    题目:http://poj.org/problem?id=2503 不知道为什么 poj  的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...

  5. poj 2503 字符串hash

    题目链接:http://poj.org/problem?id=2503 代码: #include<cstdio> #include<cstring> #include<i ...

  6. poj 2503 Babelfish(Map、Hash、字典树)

    题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...

  7. 【POJ 2503】 Babelfish

    [题目链接] http://poj.org/problem?id=2503 [算法] 字符串哈希 [代码] #include <algorithm> #include <bitset ...

  8. 【POJ 2503】Babelfish(字符串)

    题 给定字典,再询问. 字典与询问之间有一个空行. cin.peek()是一个指针指向当前字符. #include<iostream> #include<string> #in ...

  9. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

随机推荐

  1. Nginx 支持 WAF 防护功能实战

    WAF(Web Application Firewall),中文名称叫做“Web应用防火墙 WAF的定义是这样的:Web应用防火墙是通过执行一系列针对HTTP/HTTPS的安全策略来专门为Web应用提 ...

  2. CSS立体标签实现

    <style> .tag { background-color: #de3f33; position: relative; text-align: center; color: #fff; ...

  3. Nginx中的rewrite指令

    转自:http://www.76ku.cn/articles/archives/317 rewite.在server块下,会优先执行rewrite部分,然后才会去匹配location块server中的 ...

  4. 关于PHP位运算的简单权限设计

    写在最前面 最近想写一个简单的关于权限处理的东西,之前我也了解过用二进制数的位运算可以出色地完成这个任务.关于二进制数 的位运算,常见的就是“或.与.非”这三种简单运算了,当然,我也查看了下PHP手册 ...

  5. 【AngularJS】—— 7 模块化

    AngularJS有几大特性,比如: 1 MVC 2 模块化 3 指令系统 4 双向数据绑定 那么本篇就来看看AngularJS的模块化. 首先先说一下为什么要实现模块化: 1 增加了模块的可重用性 ...

  6. pyspider安装

    官方文档上说的比较简单: pip install pyspider 但是实际安装时还是有些问题导致无法成功. windows下安装 先安装PhantomJS 可以依照自己的开发平台选择不同的包进行下载 ...

  7. 【C语言入门教程】4.1 一维数组

    数组与指针涉及到数据在内存中的存储位置问题,数组由连续的存储单元组成,最低地址对应于数组的第一个单元,最高地址对应于数组的最后一个单元.指针是一种特殊的变量,该变量所存放的是内存地址,通过指针变量可访 ...

  8. indexPathForCell returns nil since ios7

    -(UITableViewCell*)GetCellFromTableView:(UITableView*)tableView Sender:(id)sender { CGPoint pos = [s ...

  9. lua操作json,mysql,redis等

    ==========================example for lua json======================= local cjson = require("cj ...

  10. js搜索框输入提示(高效-ys8)

    <style type="text/css"> .inputbox .seleDiv { border: 1px solid #CCCCCC; display: non ...