题目出处:点击打开链接
字典树?哈希表?
好吧,上面的表示真心不会,自己的思路:快排+二分查找。特别抠门的是自己一开始就被难在了输入上,要么不能达到要求,要么超时,头疼,看POJ的讨论,说“sscanf"这个函数可以达到要求,于是就用了这个,当然还有其他的输入处理可以满足要求,后面就不多说了。。。
下面为AC代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define N 100000+10
struct node
{
char s1[15];
char s2[15];
};
int cmp(const void *a,const void *b)
{
struct node *pa=(struct node *)a;
struct node *pb=(struct node *)b;
return strcmp(pa->s2,pb->s2);//as same as "return strcmp(pa->s2,pb->s2)?1:-1;"
}
int main()
{
struct node s[N];
char str[30];
int len,high,low,mid;
len=0;
while(gets(str) && str[0]!='\0')
{
sscanf(str,"%s%s",s[len].s1,s[len].s2);
len++;
}
qsort(s,len,sizeof(s[0]),cmp);
/*
for(i=0;i<len;i++)
printf("%s %s\n",s[i].s1,s[i].s2);
*/
while(scanf("%s",str)!=EOF)
{
high=len-1,low=0;
while(low<=high)
{
mid=(high+low)/2;
if(strcmp(str,s[mid].s2)==0)
break;
else if(strcmp(str,s[mid].s2)>0)
low=mid+1;
else
high=mid-1;
}
if(low<=high)
printf("%s\n",s[mid].s1);
else
printf("%s\n","eh");
}
return 0;
}