您的位置:首页 > 其它

ZOJ 1109 Language of FatMouse

2015-08-19 21:43 309 查看
题目地址:点击打开链接

思路:用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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: