【POJ 2503】Babelfish(字符串)
给定字典,再询问。
字典与询问之间有一个空行。
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(字符串)的更多相关文章
- poj 2503 Babelfish (查找 map)
题目:http://poj.org/problem?id=2503 不知道为什么 poj 的 数据好像不是100000,跟周赛的不一样 2000MS的代码: #include <iostrea ...
- poj 2503 Babelfish(Map、Hash、字典树)
题目链接:http://poj.org/bbs?problem_id=2503 思路分析: 题目数据数据量为10^5, 为查找问题,使用Hash或Map等查找树可以解决,也可以使用字典树查找. 代码( ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
- POJ 2503 Babelfish
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28766 Accepted: 12407 Descripti ...
- poj 2503 Babelfish(字典树哈希)
Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 29059 Accepted: 12565 Description You hav ...
- poj 2503 Babelfish(字典树或着STL)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 35828 Accepted: 15320 Descr ...
- Poj 2503 Babelfish(Map操作)
一.Description You have just moved from Waterloo to a big city. The people here speak an incomprehens ...
- 题解报告:poj 2503 Babelfish(map)
Description You have just moved from Waterloo to a big city. The people here speak an incomprehensib ...
- POJ 2503 Babelfish (STL)
题目链接 Description You have just moved from Waterloo to a big city. The people here speak an incompreh ...
- POJ 2503 Babelfish(map,字典树,快排+二分,hash)
题意:先构造一个词典,然后输入外文单词,输出相应的英语单词. 这道题有4种方法可以做: 1.map 2.字典树 3.快排+二分 4.hash表 参考博客:[解题报告]POJ_2503 字典树,MAP ...
随机推荐
- Linq语法详细(转)
原文地址:http://www.cnblogs.com/knowledgesea/p/3897665.html 开门见山 读这篇文章之前,我先说下,每一种搜索结果集,我都以三种方式变现出来,为啦更好的 ...
- 一行命令搞定node.js 版本升级
from:http://www.16boke.com/article/detail/26 今天,又发现一个超级简单的升级node.js的方法.一行命令搞定,省去了重新编译安装的过程. node有一个模 ...
- CF#345 (Div1)
论蒟蒻如何被cf虐 以下是身败名裂后的题解菌=========== Div1 A.Watchmen 有n个点,每个点有一个坐标.求曼哈顿距离=欧几里得距离的点对数量. 只需要统计x或y一样的点对数量. ...
- java的string常用操作
import java.util.*; public class Demo3 { public static void main(String args[]){ String str = " ...
- jquery 添加节点的几种方法介绍
<html> <head> <meta http-equiv="Content-Type" content="text/html; char ...
- 产品经理技能之MRD的笔记之一
原文:http://www.woshipm.com/pmd/131946.html/comment-page-1 产品经理技能之MRD 一.MRD与BRD的不同之处 BRD:这么做有什么好处,并说明好 ...
- codevs 1033 蚯蚓的游戏问题
Description 在一块梯形田地上,一群蚯蚓在做收集食物游戏.蚯蚓们把梯形田地上的食物堆积整理如下: a(1,1) a(1,2)…a(1,m) a(2,1) a(2,2) a(2,3)…a ...
- C#获取文件MD5字符串
备注 哈希函数将任意长度的二进制字符串映射为固定长度的小型二进制字符串.加密哈希函数有这样一个属性:在计算不大可能找到散列为相同的值的两个不同的输入:也就是说,两组数据的哈希值仅在对应的数据也匹配时才 ...
- 2016shenyang-1002-HDU5893-List wants to travel-树链剖分+线段树维护不同区间段个数
肯定先无脑树链剖分,然后线段树维护一段区间不同个数,再维护一个左右端点的费用. 线段树更新,pushDown,pushUp的时候要注意考虑链接位置的费用是否相同 还有就是树链剖分操作的时候,维护上一个 ...
- EMV内核使用中的常见问题
EMV内核在使用上会由于调用不当引起的许多问题,本文旨在基于内核LOG(也就是与IC卡交互的指令LOG)的基础上,对一些常见问题作初步的分析与解答,方便不熟悉EMV规范的同学参考. 本文的前提是你已经 ...