【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 ...
随机推荐
- javascript中String的fromCharCode()方法
前几天遇到一个bug,后端的模板引擎在输出形如: <div title="111 aaa">内容</div> 这样的内容时,无法输出' '空格,所以只能用' ...
- Use Cursor
declare : CURSOR cursor_name IS select_statement ; open : OPEN cursor_name if the query returns no r ...
- 程序流程的控制之条件分支(Delphi)
if语句主要来检测一个条件,并根据这个条件是True或者False来执行一段代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 var I: Integer ...
- winform程序重启
winform程序重启的方法: private void ReStart() { string processName = System.Diagnostics.Process.GetCurrentP ...
- IT教程网
这个IT教程网(印度),我认为是最好的.里面的知识基础实用,覆盖面很广,作为IT入门和了解都是极好的. http://www.tutorialspoint.com/
- 将数据库备份到AZURE blob storage
1创建一个Storage Account 1)点击Browse->Storage accounts 2) 填写Storage account,请记住这个名字,之后创建credential需要用到 ...
- 您的项目引用了最新实体框架;但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mysql的技巧
转载至: http://www.cnblogs.com/Imaigne/p/4153397.html 您的项目引用了最新实体框架:但是,找不到数据链接所需的与版本兼容的实体框架数据库 EF6使用Mys ...
- 一个看似很简单的SQL却难倒了很多人
一个选课表,有学生id,课程id,老师id,要求选出同时选了语文和数学的学生 USE [tempschool] GO /****** 对象: Table [dbo].[SelectC] 脚本日期: 0 ...
- .net程序员转行做手游开发经历(四)
今天是大年初二,在这里先给大家拜个年,祝大家在新的一年里能事事顺心. 年前的时候更新了一版,先发了内测,没有想到过年这几天,有的小伙伴们还在玩,还给我们提了很多建议和意见,让我们觉得非常温暖,给我们很 ...
- 年终福利,PHP7+Apache2.4+MySQL5.6 源码编译安装,环境配置,搭建你自己的LAMP环境
PHP7 都出来了,你还在玩PHP5吗? MySQL5.6 早都出来了,你还在玩MySql5.2吗? Apache2.4 早都出来了,你还在玩Apache2.2吗? 笔者不才,愿意亲自搭建环境,供搭建 ...