0
点赞
收藏
分享

微信扫一扫

ZOJ 1109 Language of FatMouse

金穗_ec4b 2022-08-04 阅读 46


题目地址:​​点击打开链接​​

思路:用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[n].english,word[n].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;
}



举报

相关推荐

0 条评论