A. Babelfish

Time Limit: 3000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

 
You have just moved from Waterloo to a big city. The people here speak an incomprehensible dialect of a foreign language. Fortunately, you have a dictionary to help you understand them.

 

Input

Input consists of up to 100,000 dictionary entries, followed by a blank line, followed by a message of up to 100,000 words. Each dictionary entry is a line containing an English word, followed by a space and a foreign language word. No foreign word appears more than once in the dictionary. The message is a sequence of words in the foreign language, one word on each line. Each word in the input is a sequence of at most 10 lowercase letters.

 

Output

Output is the message translated to English, one word per line. Foreign words not in the dictionary should be translated as "eh".

 

Sample Input

dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay atcay
ittenkay
oopslay

Sample Output

cat
eh
loops

Hint

Huge input and output,scanf and printf are recommended.
 
解题:可以用trie,不过刚学trie,写起来貌似代价很大,而且由于是先读完数据,再查询的,不是动态修改查询的,这种查询折半查找就能完成。
 
先上TLE代码,本来以为STL可以完成的,没想到超时
 
 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
map<string,string>dic;
char str[],a[],b[],word[];
int main(){
while(gets(str) && str[] != '\0'){
sscanf(str,"%s %s",a,b);
dic.insert(pair<string,string>(b,a));
}
map<string,string>::iterator it;
while(gets(word)){
if(dic.count(word)) {
it = dic.find(word);
printf("%s\n",it->second.c_str());
}else puts("eh");
}
return ;
}

下面是折半查找的代码,速度很快,代码很短,很喜欢呀。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <vector>
#include <climits>
#include <algorithm>
#include <cmath>
#include <map>
#define LL long long
#define INF 0x3f3f3f
using namespace std;
struct word {
char from[],to[];
} dic[];
bool cmp(const word &a,const word &b) {
if(strcmp(a.from,b.from) <= )
return true;
return false;
}
int bsearch(int lt,int rt,char *str) {
int mid;
while(lt <= rt) {
mid = (lt+rt)>>;
if(strcmp(dic[mid].from,str) == ) return mid;
if(strcmp(str,dic[mid].from) < ) rt = mid-;
else lt = mid+;
}
return -;
}
int main() {
int cnt = ,i,ans;
char str[];
while(gets(str) && str[] != '\0') {
sscanf(str,"%s %s",dic[cnt].to,dic[cnt].from);
cnt++;
}
sort(dic,dic+cnt,cmp);
while(gets(str)) {
ans = bsearch(,cnt,str);
if(ans == -) puts("eh");
else printf("%s\n",dic[ans].to);
}
return ;
}

好吧,补上Trie的代码,trie更快啊!叼得不行。。。。。。。。。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
struct trie {
int letter[];
bool flag[];
int index[];
};
trie dic[maxn];
char to[maxn][];
int tot,cnt,root;
char str[],a[],b[];
void insertWord(int &root,int cur,const int len,char *s) {
if(dic[root].letter[s[cur]-'a'] == )
dic[root].letter[s[cur]-'a'] = tot++;
if(cur == len-) {
dic[root].index[s[cur]-'a'] = cnt;
dic[root].flag[s[cur]-'a'] = true;
return;
}
insertWord(dic[root].letter[s[cur]-'a'],cur+,len,s);
}
int query(int root,int cur,const int len,char *s) {
if(cur == len-) {
if(dic[root].flag[s[cur]-'a'])
return dic[root].index[s[cur]-'a'];
return -;
}
if(dic[root].letter[s[cur]-'a']) {
int v = dic[root].letter[s[cur]-'a'];
return query(v,cur+,len,s);
}
return -;
}
int main() {
tot = ;
cnt = root = ;
memset(dic,,sizeof(dic));
while(gets(str)&&str[]!='\0') {
sscanf(str,"%s %s",a,b);
strcpy(to[cnt],a);
insertWord(root,,strlen(b),b);
cnt++;
}
while(gets(str)) {
int ans = query(,,strlen(str),str);
if(ans == -) puts("eh");
else puts(to[ans]);
}
return ;
}

xtu字符串 A. Babelfish的更多相关文章

  1. xtu字符串 B. Power Strings

    B. Power Strings Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java c ...

  2. xtu字符串 D. 病毒侵袭

    D. 病毒侵袭 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d      Java class nam ...

  3. xtu字符串 C. Marlon's String

    C. Marlon's String Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld      Java ...

  4. Babelfish(二分查找,字符串的处理略有难度,用sscanf输入)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 28581   Accepted: 12326 题目链接: ...

  5. POJ2503——Babelfish(map映射+string字符串)

    Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...

  6. XTU OJ 1207 Welcome to XTCPC (字符串签到题)

    Problem Description Welcome to XTCPC! XTCPC start today, you are going to choose a slogan to celebra ...

  7. 【POJ 2503】Babelfish(字符串)

    题 给定字典,再询问. 字典与询问之间有一个空行. cin.peek()是一个指针指向当前字符. #include<iostream> #include<string> #in ...

  8. Kattis - Babelfish

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

  9. Python高手之路【六】python基础之字符串格式化

    Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...

随机推荐

  1. Android的代码适配方案

    public class DensityUtil { private DensityUtil(){ throw new AssertionError(); } /** * dp转px * @param ...

  2. jQuery attr() 源码解读

    我们知道,$().attr()实质上是内部调用了jQuery.access方法,在调用时jQuery.attr作为回调传入.在通过种种判断(参看jQuery.access()方法)之后,取值和赋值最后 ...

  3. js修改物理返回键功能

    preventBack: function(theurl){ var pushState = window.history.pushState; //点击物理返回键时,退出到跳转定义首页 if(pus ...

  4. appium学习链接记录

    乙醇大师的园子: http://www.cnblogs.com/nbkhic/tag/appium/ webDriver java版 https://github.com/easonhan007/we ...

  5. knockout Observable Array(监控数组)

    Observable Array(监控数组)的作用 列表操作是经常会遇到的一个场景,使用监控数组,你可以: 保存列表对象,并且使用Ko提供的丰富的API操作列表元素(支持内建js Array的方法,以 ...

  6. ssm框架搭建(下) 简单案例

    前言 这段时间没有更新博客,一直想做一个基于ssm的简单的项目.经过多次的尝试,终于实现了简单的增删查改功能了. 正文 由于前端的技术不是很熟悉,经过多方的查阅,使用了bootstrap的样式,来使界 ...

  7. Centos5安装***

    最近shadowsocks挺火,看了几张帖子,感觉在手机上应该挺好用,电脑都是挂着ssh,用不到***了,下面贴出服务器安装过程: yum install build-essential autoco ...

  8. css3纯手写loading效果

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  9. 11G RAC环境数据库启动和关闭

    一步启动Oracle (1) 启动整个集群 # ./crsctl start cluster -all -all选项启动整个集群. 不加-all选项只能启动本节点的服务. (2) 启动本节点集群 以下 ...

  10. ElasticSearch数据库同步插件logstash

    1.下载和elasticsearch 相同版本的logstash. 2.进行解压后,进入bin下,新建一个文件mysql.conf,并输入 input { stdin{ }} output { std ...