xtu字符串 A. Babelfish
A. Babelfish
64-bit integer IO format: %lld Java class name: Main
Input
Output
Sample Input
dog ogday
cat atcay
pig igpay
froot ootfray
loops oopslay atcay
ittenkay
oopslay
Sample Output
cat
eh
loops
Hint
#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的更多相关文章
- xtu字符串 B. Power Strings
B. Power Strings Time Limit: 3000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java c ...
- xtu字符串 D. 病毒侵袭
D. 病毒侵袭 Time Limit: 1000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d Java class nam ...
- xtu字符串 C. Marlon's String
C. Marlon's String Time Limit: 2000ms Memory Limit: 65536KB 64-bit integer IO format: %lld Java ...
- Babelfish(二分查找,字符串的处理略有难度,用sscanf输入)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 28581 Accepted: 12326 题目链接: ...
- POJ2503——Babelfish(map映射+string字符串)
Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...
- XTU OJ 1207 Welcome to XTCPC (字符串签到题)
Problem Description Welcome to XTCPC! XTCPC start today, you are going to choose a slogan to celebra ...
- 【POJ 2503】Babelfish(字符串)
题 给定字典,再询问. 字典与询问之间有一个空行. cin.peek()是一个指针指向当前字符. #include<iostream> #include<string> #in ...
- Kattis - Babelfish
Babelfish You have just moved from Waterloo to a big city. The people here speak an incomprehensible ...
- Python高手之路【六】python基础之字符串格式化
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存.[PEP-3101] This ...
随机推荐
- 用 Fleck 实现 websocket 通信
<html lang="en"> <head> <meta charset="utf-8"> <title>rf ...
- [转]Android 如何监听返回键,弹出一个退出对话框
本文转自:http://blog.csdn.net/sunnyfans/article/details/8094349 Android 如何监听返回键点击事件,并创建一个退出对话框, 防止自己写的应用 ...
- Ubuntu16下查看CPU、内存和磁盘相关信息
1.内存 查看内存#free -m total used free shared buff/cache available Mem: Swap: 2.CPU 查看逻辑cpu个数: #cat /proc ...
- 复位电路设计——利用PLL锁定信号(lock)产生复位信号
利用PLL锁定信号(lock)产生复位信号 在FPGA刚上电的时候,系统所需的时钟一般都要经过PLL倍频,在时钟锁定(即稳定输出)以前,整个系统应处于复位状态.因此,我们可以利用PLL的锁定信号来产生 ...
- java实现汉诺塔算法
package com.ywx.count; import java.util.Scanner; /** * @author Vashon * date:20150410 * * 题目:汉诺塔算法(本 ...
- Centos5安装***
最近shadowsocks挺火,看了几张帖子,感觉在手机上应该挺好用,电脑都是挂着ssh,用不到***了,下面贴出服务器安装过程: yum install build-essential autoco ...
- 洛谷 P3038 [USACO11DEC]牧草种植Grass Planting
题目描述 Farmer John has N barren pastures (2 <= N <= 100,000) connected by N-1 bidirectional road ...
- Hadoop分布式集群安装
环境准备 操作系统使用ubuntu-16.04.2 64位 JDK使用jdk1.8 Hadoop使用Hadoop 2.8版本 镜像下载 操作系统 操作系统使用ubun ...
- SQLite_Home
SQLite教程 SQLite是一个库,实现了一个独立的软件,serverless zero-configuration.事务SQL数据库引擎.SQLite是世界上最广泛的部署SQL数据库引擎.SQL ...
- JDO
JDO 编辑 本词条缺少名片图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! JDO(Java Data Object )是Java对象持久化的新的规范,也是一个用于存取某种数据仓库中的对象 ...