描述:快排+二分查找
#include <cstring>
#include <cstdlib>
#include <cstdio>
struct Sea
{
char w[13];
char exp[21];
};
Sea str[100010];
int cmp(const void *p1,const void *p2)
{
return strcmp(((Sea*)p1)->exp , ((Sea*)p2)->exp);
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("a.txt", "r", stdin);
#endif
char s[110];
int flag=0,len,count=0;
memset(s,0,sizeof(s));
while(gets(s)!=NULL)
{
len=strlen(s);
if(len<1) break;
for(int i=0; i<len; i++)
if(s[i]!=' ') str[flag].w[count++]=s[i];
else
{
str[flag].w[count]=0;
count=0;
for(int j=i+1; j<len; j++)
str[flag].exp[count++]=s[j];
flag++;
count=0;
break;
}
memset(s,0,sizeof(s));
}
qsort(str,flag,sizeof(Sea),cmp);
count=0;
while(scanf("%s",s)!=EOF)
{
int front=0,rear=flag-1;
while(front<=rear)
{
len=(front+rear)/2;
if(strcmp(s,str[len].exp)>0) front=len+1;
else if(strcmp(s,str[len].exp)<0) rear=len-1;
else
{
printf("%s\n",str[len].w);
count=1;
break;
}
}
if(!count) printf("eh\n");
count=0;
memset(s,0,sizeof(s));
}
return 0;
}
10282 - Babelfish
最新推荐文章于 2021-02-13 22:15:30 发布