hdu1880(魔咒词典)

http://acm.hdu.edu.cn/showproblem.php?pid=1880

 

1.下面是二分查找(ac)代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>

#define max 100005

typedef struct Node
{
 char mo[25];
 char yu[85];
}Node;

Node mag1[max];
Node mag2[max];

int cmp1(const void *a,const void *b)
{
      Node *c=(Node *)a;
   Node *d=(Node *)b;
      return strcmp(c->mo,d->mo);
}

int cmp2(const void *a,const void *b)
{
      Node *c=(Node *)a;
   Node *d=(Node *)b;
      return strcmp(c->yu,d->yu);
}

int search(char str[],int len,int tag) //返回查找到的元素在队列中的位置
{
 int be=0,en=len-1,t;
    while(be<=en)
 {
  t=(be+en)/2;
       if(tag==0)
    {
          if(strcmp(str,mag1[t].mo)<0)
     en=t-1;
    else if(strcmp(str,mag1[t].mo)>0)
              be=t+1;
    else
     return t;
    }
    else
    {
          if(strcmp(str,mag2[t].yu)<0)
     en=t-1;
    else if(strcmp(str,mag2[t].yu)>0)
              be=t+1;
    else
     return t;
    }
 }
 return -1;
}

int main()
{
 char str[120];
 int count=0,f;
 int i,len;
 while(gets(str))
 {
  if(strcmp(str,"@END@")==0) break;
  for(i=0;i<strlen(str);i++)
   if(str[i]==']')
    break;
         strncpy(mag1[count].mo,str,i+1);
   strcpy(mag1[count].yu,str+i+2);
   strcpy(mag2[count].mo,mag1[count].mo);
   strcpy(mag2[count].yu,mag1[count].yu);
   count++;
 }
    qsort(mag1,count,sizeof(Node),cmp1);
 qsort(mag2,count,sizeof(Node),cmp2);

 int n;
 scanf("%d",&n);
 getchar();
 for(i=0;i<n;i++)
 {
  gets(str);
  if(str[0]=='[')
  {
            f=search(str,count,0);
   if(f>=0)
    printf("%s/n",mag1[f].yu);
   else
    printf("what?/n");
  }
  else
  {
   f=search(str,count,1);
   len=strlen(mag2[f].mo);
   mag2[f].mo[len-1]='/0';
   if(f>=0)
    printf("%s/n",mag2[f].mo+1);
   else
    printf("what?/n");
  }
 }
 return 0;
}

 

2.下面是采用STL map方法代码,不过在杭电acm上会发生memory limit exceeded,不过还是贴出来看看:

#include<iostream>
#include<map>
#include<string>
using namespace std;

map<string,string> ma1;
map<string,string> ma2;

int main()
{
 int tag,len,n;
 string str,str1,str2,str3;
 map<string,string>::iterator iter;
    char ch[120];
 while(gets(ch))
 {
  if(strcmp(ch,"@END@")==0) break;
  str=ch;
        tag=str.find(']');
  len=str.length();
        str1=str.substr(0,tag+1);
  str2=str.substr(tag+2,len-tag);
  str3=str.substr(1,tag-1);
  ma1.insert(map<string,string>::value_type(str1,str2));
  ma2.insert(map<string,string>::value_type(str2,str3));
 }
   
 scanf("%d",&n);
 getchar();
 for(int i=0;i<n;i++)
 {
  gets(ch);
  str=ch;
  if(ch[0]=='[')
  {
           iter=ma1.find(str);
     if(iter!=ma1.end())
      cout<<iter->second<<endl;
        else
      cout<<"what?"<<endl;
  }
  else
  {
   iter=ma2.find(str);
            if(iter!=ma2.end())
      cout<<iter->second<<endl;
        else
      cout<<"what?"<<endl;
  }
  
 }
 return 0;
}

感受:做题时要看清题目,我是因为没去[和]无法ac郁闷了很长一段时间!!!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值