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. Tomcat编码问题及访问软链接文件设置

    Tomcat编码问题及访问软链接文件设置 一.编码问题:让其支持UTF-8格式 修改tomcat中server.xml Connector port=" protocol="org ...

  2. Python基础之【第一篇】

    Python简介: python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语 ...

  3. string.replace正则表达式说明

    str.replace(reg,function($0,$1,$2...,index,str){ }); $0: 匹配模式的字符串$1...: 匹配模式子表达式的字符串,0个或多个,个数取决于子表达式 ...

  4. webstorm总结

    webStorm修改文件类型,文件着色 File->Settings->Editor->File Types

  5. SQL having 子句

    1.为什么存在? 在 SQL 中增加 HAVING 子句原因是,WHERE 关键字无法与合计函数一起使用. 2.举例子: SELECT Customer,SUM(OrderPrice) FROM Or ...

  6. Linux启动管理:grub

    1.grub中分区表示 Linux 中 /dev/sda1   在grub中为   hd0,0    代表第一个硬盘的第一个分区 Linux中 /dev/sdb3是扩展分区     在grub中为   ...

  7. socket编程报异常java.io.EOFException

    一个客户端连接服务器的小程序,服务器端可以正常读取客户端发来的数据 但是当客户端关闭时,服务端也关闭了,并且抛出如下的异常: java.io.EOFException at java.io.DataI ...

  8. 让我们一起学Node.js-文章列表

    新浪的博客最近不给力,只好在博客园落个窝.至此之后,技术随笔会在博客园以及新浪的博客上同时更新,如果新浪给力的话~~~ 如果你想看先前新浪博客上分享的技术,请点击此处 忘尘子新浪博客! 我是拜读了朴灵 ...

  9. data_quick 进度

    20131118  加入xml对L8卫星波段控制: 纠正波段顺序 QVProc20131125.7z: 完成L8的增强 以后的程序会添加ZY3号解析模块 QVProc20131126.7z: 新增 粗 ...

  10. iOS开发——UI进阶篇(十八)核心动画小例子,转盘(裁剪图片、自定义按钮、旋转)图片折叠、音量震动条、倒影、粒子效果

    一.转盘(裁剪图片.自定义按钮.旋转) 1.裁剪图片 将一张大图片裁剪为多张 // CGImageCreateWithImageInRect:用来裁剪图片 // image:需要裁剪的图片 // re ...