ZOJ 1109 Language of FatMouse
2015-08-19 21:43
309 查看
题目地址:点击打开链接
思路:用STL容易超时,本题没有超时,用字典树比较麻烦,可以先排序再二分搜索
AC代码:
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct node
{
char english[20],mouse[20];
}word[100105];
int cmp(const void *_a,const void *_b)//按字符串升序排列
{
node *a = (node*)_a;
node *b = (node*)_b;
return strcmp(a->mouse,b->mouse);
}
int main()
{
char c[40];
int n = 0;
while(gets(c))
{
if(strlen(c) == 0)
break;
sscanf(c,"%s%s",word
.english,word
.mouse);
n++;
}
qsort(word,n,sizeof(node),cmp);
int left,right,mid,ok;
while(scanf("%s",c) != EOF)
{
left = ok = 0;
right = n - 1;
while(left <= right)
{
mid = (left + right) / 2;
if(strcmp(word[mid].mouse,c) == 0)
{
ok = 1;
break;
}
else if(strcmp(word[mid].mouse,c) > 0)
right = mid - 1;
else
left = mid + 1;
}
if(ok)
printf("%s\n",word[mid].english);
else
printf("eh\n");
}
return 0;
}
思路:用STL容易超时,本题没有超时,用字典树比较麻烦,可以先排序再二分搜索
AC代码:
#include <iostream> #include <cstdio> #include <cstring> #include <map> using namespace std; int main() { map<string,string> entry; char a[20],b[20],c[40]; string value,key; while(gets(c)) { if(strlen(c) == 0) break; sscanf(c,"%s%s",a,b); value = a; key = b; entry[key] = value; } while(scanf("%s",a) != EOF) { if(entry.find(a) != entry.end()) //printf("%s\n",entry[a]);不能用print输出 cout<<entry[a]<<endl; else printf("eh\n"); } return 0; }
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
using namespace std;
struct node
{
char english[20],mouse[20];
}word[100105];
int cmp(const void *_a,const void *_b)//按字符串升序排列
{
node *a = (node*)_a;
node *b = (node*)_b;
return strcmp(a->mouse,b->mouse);
}
int main()
{
char c[40];
int n = 0;
while(gets(c))
{
if(strlen(c) == 0)
break;
sscanf(c,"%s%s",word
.english,word
.mouse);
n++;
}
qsort(word,n,sizeof(node),cmp);
int left,right,mid,ok;
while(scanf("%s",c) != EOF)
{
left = ok = 0;
right = n - 1;
while(left <= right)
{
mid = (left + right) / 2;
if(strcmp(word[mid].mouse,c) == 0)
{
ok = 1;
break;
}
else if(strcmp(word[mid].mouse,c) > 0)
right = mid - 1;
else
left = mid + 1;
}
if(ok)
printf("%s\n",word[mid].english);
else
printf("eh\n");
}
return 0;
}
相关文章推荐
- POJ1850——Code
- ubuntu12.04更新时签名错误(BADSIG 40976EAF437D05B5)
- poj1734(floyd算法)
- SVN使用小结
- Java编程思想读书笔记:第2章 一切都是对象
- [150130] 1/7の魔法使い【日文硬盘版】带BT种子镜像版(带全CG存档+转码打开补丁+存档文件夹补丁)
- json数据转化成模型对象
- [洛谷1314]无序字母对
- SVN使用小结
- phaser游戏开发之基础知识1
- Sphinx多线程分布式搜索
- STL使用:deque双端队列
- Android性能优化(一),总纲
- LeetCode 27 Remove Element
- Intent
- oracle中游标的属性和修改默认游标的大小
- ORACLE 存储函数
- 8.18 [LeetCode 52] N-Queens II
- 【string】KMP, 扩展KMP,trie,SA,ACAM,SAM,最小表示法
- LeetCode:Longest Substring Without Repeating Characters(update)