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. ForeignKeyConstraint 外键约束的使用及作用的学习[转]

    原文链接 da.SelectCommand.CommandText="select au_id,au_fname,au_lname from authors"; da.Fill(d ...

  2. pyspark中使用累加器Accumulator统计指标

    评价分类模型的性能时需要用到以下四个指标 最开始使用以下代码计算,发现代码需要跑近一个小时,而且这一个小时都花在这四行代码上 # evaluate model TP = labelAndPreds.f ...

  3. Asp.Net MVC中捕捉错误路由并设置默认Not Found页面。

    在Global中写一个Application_Error捕捉错误路由并重定向到Not Found页面.这里是全局性抓取错误路由,此处还可以写由错误路由导致访问失败的日志记录. protected vo ...

  4. (译文)IOS block编程指南 4 声明和创建blocks

    Declaring and Creating Blocks (声明和创建blocks) Declaring a Block Reference (声明一个block引用) Block variable ...

  5. Python3基础教程(十五)—— PEP8 代码风格指南

    编程语言不是艺术,而是工作或者说是工具,所以整理并遵循一套编码规范是十分必要的. 这篇文章原文实际上来自于这里:https://www.python.org/dev/peps/pep-0008/ 有很 ...

  6. uva11491 Erasing and Winning

    边读入边处理 优化了速度一开始有想错了的地方.处理输入有点想用stringstream, 的问题在于他把字符串连续的数字作为一个整体,遇到空格才分开,所以不适用 #include<cstdio& ...

  7. Object.assign(o1, o2, o3) 对象 复制 合拼

    Object 对象方法学习之(1)—— 使用 Object.assign 复制对象.合并对象 合并对象 var o1 = {a: 1}; var o2 = {b: 2}; var o3 = {c: 3 ...

  8. 【转】Delphi 文件读写

    procedure TForm1.Button1Click(Sender: TObject); variFileHandle: Integer;iFileLength: Integer;iBytesR ...

  9. JavaScript/JQuery radioButton(单选按钮)练习20190409

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

  10. POJ-1190-生日蛋糕(深搜,剪枝)

    生日蛋糕 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23049 Accepted: 8215 Description 7月1 ...