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. Python之路【第三篇】:Python基础(二)

    函数的理解 面向过程:根据业务逻辑从上到下写垒代码 函数式:将某功能代码封装到函数中,日后便无需重复编写,仅调用函数即可 函数作用是你的程序有良好的扩展性.复用性. 同样的功能要是用3次以上的话就建议 ...

  2. sql语句积累

    有一个需求表(demand),每一记录就是一条需求:另外有一个报价表(quotation),每一条记录是对需求记录的报价详情. 需求表: 报价表: 我现在想得到每条需求的信息以及有多少人报价了,我们可 ...

  3. Hadoop快速入门

    目的 这篇文档的目的是帮助你快速完成单机上的Hadoop安装与使用以便你对Hadoop分布式文件系统(HDFS)和Map-Reduce框架有所体会,比如在HDFS上运行示例程序或简单作业等. 先决条件 ...

  4. 离屏渲染学习笔记 /iOS圆角性能问题

    离屏渲染学习笔记 一.概念理解 OpenGL中,GPU屏幕渲染有以下两种方式: On-Screen Rendering 意为当前屏幕渲染,指的是GPU的渲染操作是在当前用于显示的屏幕缓冲区中进行. O ...

  5. VS2010webConfig配置

    1.连接SqlServer数据库 <connectionStrings> <add name="ConnectionStringName" connectionS ...

  6. 基于zepto判断mobile的横竖屏状态

    借用jquery mobile中的代码,删除了一些多余的部分,简单的基于zepto的模块 var CheckOrientation = (function(){ var win = $( window ...

  7. R中的name命名系列函数总结

    本文原创,转载请注明出处,本人Q1273314690 R中关于给行列赋名称的函数有 dimnames,names,rowname,colname,row.names 这五个函数,初学的时候往往分不清楚 ...

  8. 安装cocopods

    http://www.tuicool.com/articles/7VvuAr3 OS 最新版 CocoaPods 的安装流程 1.移除现有Ruby默认源 $gem sources --remove h ...

  9. nyoj 289 苹果 动态规划 (java)

    分析:0-1背包问题 第一次写了一大串, 时间:576  内存:4152 看了牛的代码后,恍然大悟:看来我现在还正处于鸟的阶段! 第一次代码: #include<stdio.h> #inc ...

  10. POJ 3252 Round Numbers

     组合数学...(每做一题都是这么艰难) Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7607 A ...