九度OJ 题目1029:魔咒词典 (二)

2014-11-24 07:54:58 · 作者: · 浏览: 1
on(str);
}
scanf("%d\n",&n);
for(i = 0;i < n;i++){
gets(s);
str = s;
//寻找魔咒或者对应功能
if(Map.find(str) == Map.end())
{
printf("what \n");
}
else
{
str = Map[str];
//输出对应的功能
if(str[0] != '[')
{
cout<
}
//输出对应的魔咒
else
{
cout<
}
}
}//for
return 0;
}转载
[cpp]
#include
#include
#include
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
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;
}
#include
#include
#include
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]),cmp