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 ...
随机推荐
- 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String
题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...
- springboot 配置Ehcache
Ehcache的基本配置说明我就不说了.小编记录一下在springboot中使用Ehcache的使用方法. 第一步:在classpath下引入配置文件ehcache.xml 代码如下: <ehc ...
- Java GUI 布局管理器
容器可设置布局管理器,管理容器中组件的布局: container.setLayout(new XxxLayout()); Java有6种布局管理器,AWT提供了5种: FlowLayout Borde ...
- Java基础--java简介
1.Java的起源: Oak --> Java 2.Java的发展 Java1.0 Java2 JavaSE:Java平台标准版 JavaME:微型版 JavaEE:企业版 Sun公司 or ...
- 在git远程仓创建项目之后,提交本地项目的使用方法
命令介绍 git 用户配置 git config --global user.name "张三" git config --global user.email "zhag ...
- 46 Simple Python Exercises-Higher order functions and list comprehensions
26. Using the higher order function reduce(), write a function max_in_list() that takes a list of nu ...
- C# 移动开发(Xamarin.Form) Plugin.BLE 蓝牙连接
随着Xamarin.Form项目接近尾声,仔细一算才发现过来大半年时间了. 期间除了刚开始有闲情写写,现在总算有空来总结一下了. 来先说 Plugin.BLE (https://github.com/ ...
- [Github筆記] 清除所有 Commit 紀錄
# 把原來的 git 移除掉 sudo rm .git -r # 初始化 git init git remote add origin https://github.com/username/repo ...
- ML-学习提纲2
https://machinelearningmastery.com/a-tour-of-machine-learning-algorithms/ http://blog.csdn.net/u0110 ...
- nginx 的编译安装及基本操作
下载nginx [root@nginx ~]# wget http://nginx.org/download/nginx-1.14.0.tar.gz --2019-05-02 21:52:23-- h ...