题目1029:魔咒词典

#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
****************************************************************/


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值