#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct dic
{
char wo[22];
char ex[82];
}a[10000],b[10000];
int cmp1(const void *a,const void *b)
{
return strcmp((*(struct dic *)a).wo,(*(struct dic *)b).wo);
}
int cmp2(const void *a,const void *b)
{
return strcmp((*(struct dic *)a).ex,(*(struct dic *)b).ex);
}
int main()
{
int i,k;
int n,high,low,mid,tmp,ans;
char s[104],c[82];
k=0;
while(gets(s)&&strcmp(s,"@END@"))
{
int j=0;
i=1;
while(s[i]!=']')
{
a[k].wo[j]=s[i];
b[k].wo[j]=s[i];
j++;
i++;
}
a[k].wo[j+1]='\0';
b[k].wo[j+1]='\0';
if(s[i]==']') i+=2;
j=0;
while(s[i]!='\0')
{
a[k].ex[j]=s[i];
b[k].ex[j]=s[i];
j++;
i++;
}
a[k].ex[j+1]='\0';
b[k].ex[j+1]='\0';
k++;
}
qsort(a,k,sizeof(a[0]),cmp1);
qsort(b,k,sizeof(b[0]),cmp2);
scanf("%d",&n);
getchar();
while(n--)
{
gets(c);
ans=-1;
low=0;high=k-1;
if(c[0]=='[')
{
int len;
len=strlen(c);
for(i=0;i<len-2;i++)
c[i]=c[i+1];
c[i]='\0';
while(low<=high)
{
mid=(low+high)/2;
tmp=strcmp(c,a[mid].wo);
if (tmp==0) {ans=mid;break;}
else if(tmp>0) low=mid+1;
else high=mid-1;
}
if(ans==-1) printf("what?\n");
else printf("%s\n",a[ans].ex);
}
else
{
while(low<=high)
{
mid=(low+high)/2;
tmp=strcmp(c,b[mid].ex);
if (tmp==0) {ans=mid;break;}
else if(tmp>0) low=mid+1;
else high=mid-1;
}
if(ans==-1) printf("what?\n");
else printf("%s\n",b[ans].wo);
}
}
return 0;
}
/**************************************************************
Problem: 1029
User: cust123
Language: C++
Result: Accepted
Time:10 ms
Memory:3052 kb
****************************************************************/
题目1029:魔咒词典
最新推荐文章于 2019-11-16 23:26:08 发布