给定字典,再询问。

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

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. [No00002A]26个英语字母的原始象形意义、含义、产生及发展历史

    我们都知道汉字是象形文字,但如果说英语也是象形文字,你一定会以为纯是无稽之谈.其实,追根溯源,英语的26个字母确实来自于象形文字.这26个字母最初起源于埃及象形文字,后由腓尼基人改进发明了腓尼基字母, ...

  2. 理解Java中字符流与字节流的区别

    1. 什么是流 Java中的流是对字节序列的抽象,我们可以想象有一个水管,只不过现在流动在水管中的不再是水,而是字节序列.和水流一样,Java中的流也具有一个“流动的方向”,通常可以从中读入一个字节序 ...

  3. luogu10125回文数[noip1999 Day1 T1]

    题目描述 若一个数(首位不为零)从左向右读与从右向左读都一样,我们就将其称之为回文数. 例如:给定一个10进制数56,将56加65(即把56从右向左读),得到121是一个回文数. 又如:对于10进制数 ...

  4. c++ 基于wincrypt的DES CBC模式加解密

    des.h #pragma once #include <windows.h> #include <atlstr.h> #include <wincrypt.h> ...

  5. react拷贝index.html很恶心之解决办法

    https://www.npmjs.com/package/html-webpack-plugin

  6. FastFourierTransform (FFT)

    FastFourierTransform.h #pragma once #include <stdio.h> #include <math.h> #ifndef INCLUDE ...

  7. Java 集合与队列的插入、删除在并发下的性能比较

    这两天在写一个java多线程的爬虫,以广度优先爬取网页,设置两个缓存: 一个保存已经访问过的URL:vistedUrls 一个保存没有访问过的URL:unVistedUrls 需要爬取的数据量不大,对 ...

  8. 调试记录:The public type <<classname>> must be defined in its own file

    (摘自stackoverflow) “The public type <<classname>> must be defined in its own file” error ...

  9. 前端框架——BootStrap学习

    BootStrap简单总结下:1.栅格系统,能够很好的同时适应手机端和PC端(及传说中的响应式布局) 2.兼容性好 接下来是对BootStrap学习的一些基础案例总结和回顾: 首先引入:bootstr ...

  10. iOS -- cocopods使用