给定字典,再询问。

字典与询问之间有一个空行。

cin.peek()是一个指针指向当前字符。

#include<iostream>
#include<string>
#include<map>
using namespace std;
map<string, string>dic;
string s, t;
int f;
int main()
{
ios::sync_with_stdio(false);
while(cin >> s)
if(cin.peek() == '\n')//指针当前指向\n
if(!f)
{
f = ;
dic[s] = t;//当前读入的s是外星文,刚才读入的t是对应的英文
}
else
cout << ((t = dic[s]) != "" ? t : "eh" ) << "\n";
else//未换行说明s是字典的英文
{
f = ;//f=0代表下一个要读外星文
t = s;
}
}

处理空行的技巧

#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 100005
#define M 15
char s[N];
int c;
struct mydic
{
char zh[M];//外星文
char en[M];//英文
} dic[N];
int cmp(const mydic &a, const mydic &b)
{
return strcmp(a.zh, b.zh) >= ;
}
void solve()
{
int l = , r = c;
while(l < r)
{
int m = r+l >> ;
int f = strcmp(s, dic[m].zh);
if(f == )
{
printf("%s\n", dic[m].en);
return;
}
if(f < )
l = m + ;
else
r = m;
}
printf("eh\n");
}
int main()
{
while(gets(s))
{
if(!s[])break;//读到空行
sscanf(s,"%s%s",dic[c].en,dic[c].zh);
c++;
}
sort(dic, dic + c, cmp);
while(gets(s))
solve();
}

  

  

【POJ 2503】Babelfish(字符串)的更多相关文章

  1. poj 2503 Babelfish (查找 map)

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

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

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

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

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

  4. POJ 2503 Babelfish

    Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28766 Accepted: 12407 Descripti ...

  5. poj 2503 Babelfish(字典树哈希)

    Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 29059 Accepted: 12565 Description You hav ...

  6. poj 2503 Babelfish(字典树或着STL)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 35828   Accepted: 15320 Descr ...

  7. Poj 2503 Babelfish(Map操作)

    一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...

  8. 题解报告:poj 2503 Babelfish(map)

    Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...

  9. POJ 2503 Babelfish (STL)

    题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...

  10. POJ 2503 Babelfish(map,字典树,快排+二分,hash)

    题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...

随机推荐

  1. [转] 如何设置Eclipse的上网代理

    from: http://blog.csdn.net/qq635785620/article/details/8191799 不同版本的eclipse有不同的设置方法 方式一:   默认的Eclips ...

  2. 渗透中Necat的另类用法

    Necat 是一个伟大而实用的用于 TCP 和 UPD 网络连接协议的读写程序.同时 Necat 也被誉为网络中的瑞士军刀,在许多黑客教程中 Necat 也被广泛使用.Necat 最常见用途是设置反向 ...

  3. BZOJ 1101: [POI2007]Zap

    1101: [POI2007]Zap Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2262  Solved: 895[Submit][Status] ...

  4. javascript中使用循环链表实现约瑟夫环问题

    1.问题 传说在公元1 世纪的犹太战争中,犹太历史学家弗拉维奥·约瑟夫斯和他的40 个同胞被罗马士兵包围.犹太士兵决定宁可自杀也不做俘虏,于是商量出了一个自杀方案.他们围成一个圈,从一个人开始,数到第 ...

  5. S2总结笔记

    第一章:深入.NET框架 1..NET FrameWork两大组件是什么? 解析:.NET 框架类库(FCL:FrameWork Class Library)和公共语言运行时(CLR:common l ...

  6. Centos 检查磁盘读写性能

    启动Tomcat发现deploy war的速度明显变慢, 怀疑磁盘出问题 测试写入 [tomcat@localhost ~]$ dd if=/dev/zero of=kwxgd bs=64k coun ...

  7. BZOJ 1014 【JSOI2008】 火星人prefix

    Description 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 ...

  8. 简单创建与布署CLR存储过程

    今天的博文是学习CLR存储过程,一个简单的例子,学会怎样创建,编译,布署在SQL中.CLR能做一些T-SQL无法做的事情,很多情况之后,它比T-SQL快. 打开VS2013,创建一个新专案,参考下面5 ...

  9. Android动画学习笔记-Android Animation

    Android动画学习笔记-Android Animation   3.0以前,android支持两种动画模式,tween animation,frame animation,在android3.0中 ...

  10. 生成 PDF 全攻略【1】初体验

    经历过多少踩坑,翻看过多少类似博客,下载过多少版本的Jar,才能摸索出正确的代码书写方式,才能实现项目经理需求分析书中的功能点. 本文借一次 JavaEE 生成PDF的颠簸的实现过程,描述中小公司程序 ...