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. python_函数嵌套(4)

    第1章 名称空间 1.1 定义 1.2 变量运行流程 1.3 临时名称空间 1.4 python三种名称空间 第2章 作用域 2.1 作用域分类 2.2 加载顺序 2.3 取值顺序 函数嵌套 2.4 ...

  2. 单机版solr6.3和分布式solr6.3的安装部署

    一.单机版的solr部署 我的是在windows下安装的,linux同理 1. 安装JDK8,并配置好环境变量,一般我们经常开发的电脑上应该都有JDk了,所以这一步可以忽略. 2. 解压solr6.3 ...

  3. 项目错误提示Multiple markers at this line

    新安装个Myeclipse,导入以前做的程序后程序里好多错,第一行提示: Multiple markers at this line         - The type java.lang.Obje ...

  4. dangerouslySetHTML 和 style 属性

    这一节我们来补充两个之前没有提到的属性,但是在 React.js 组件开发中也非常常用,但是它们也很简单. dangerouslySetHTML 出于安全考虑的原因(XSS 攻击),在 React.j ...

  5. RabbitMQ九:远程过程调用RPC

    定义 RPC(Remote Procedure Call Protocol)——远程过程调用协议:它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协议.RPC协议假定某些传输协议 ...

  6. AJPFX简述JavaStringBuffer方法

    以下是StringBuffer类支持的主要方法: 序号 方法描述 1 public StringBuffer append(String s)将指定的字符串追加到此字符序列. 2 public Str ...

  7. AJPFX实例集合嵌套之ArrayList嵌套ArrayList

    案例:import java.util.ArrayList;import java.util.Iterator;import com.heima.bean.Person;public class De ...

  8. 解决Android Studio安装过程中“SDK tools directory is missing”的问题

    "SDK tools directory is missing",这是因为安装时你的计算机无法连接到google的服务器(对google服务器的域名地址解析出问题了),无法从goo ...

  9. eclipse搭建android开发环境详细步骤

    搭建android应用的开发环境,一套程序下来也是相当繁琐的,这里我整理下一整套详细流程: 1,下载JDK 去oracle官网下载最新版本的jdk,官网地址 http://www.oracle.com ...

  10. vSphere Client用户名密码保存记录

    vSphere Client在访问ESXi主机或vCenter后是默认不保存登录用户名和密码的,不过可以通过修改配置文件来保存,方便访问连接. 方法如下: 打开配置文件路径(实际安装路径):D:\Pr ...