HDU 1880

hash表版

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




using namespace std;
#define mod 100007
#define N 100010


struct node
{
    char que[25];
    char ans[85];
    int next;
};


node Hasha[N], Hashb[N];
int heada[N], headb[N];
int cnta, cntb;


unsigned int getindex( char *str)
{
    unsigned int ans = 0;
    unsigned int seed = 131;
    while(*str)
     ans = ans * seed + *str++;
    return (ans & 0x7fffffff) % mod;
}


void init()
{


    for( int i = 0; i < N; i++)
       heada[i] = headb[i] = -1;
    cnta = cntb = 0;
}


void insert(char* que, char* ans)
{
    unsigned int h;
    h = getindex(que);
    strcpy(Hasha[cnta].que, que);
    strcpy(Hasha[cnta].ans, ans);
    Hasha[cnta].next = heada[h];
    heada[h] = cnta;
    ++cnta;


    h = getindex(ans);
    strcpy(Hashb[cntb].que, que);
    strcpy(Hashb[cntb].ans, ans);
    Hashb[cntb].next = headb[h];
    headb[h] = cntb;
    ++cntb;
}


int search_Hasha( char * a)
{
   // cout<<"search a "<<endl;
    unsigned int h = getindex(a);


    for( int i = heada[h]; i != -1; i = Hasha[i].next)
    {
       if(strcmp(Hasha[i].que, a) == 0)
          return i;
    }
    return -1;
}


int search_Hashb( char *b)
{
    //cout<<"search b"<<endl;
    unsigned int h = getindex(b);
    for( int i = headb[h]; i != -1; i = Hashb[i].next)
    {
        if(strcmp(Hashb[i].ans, b) == 0)
         return i;
    }
  return -1;
}


int main()
{
        //freopen("a.txt","r",stdin);
        init();
        char str[120];
        char a[25];
        char op[85];
        int que;
        while(gets(str))
        {


          if(!strcmp(str,"@END@"))  break;
          int i = 1;
          int pos1  = 0, pos2 = 0;


          while(str[i] != ']')
            a[pos1++] = str[i++];
          a[pos1] = '\0';


          i+=2;
          while(str[i] != '\0')
            op[pos2++] = str[i++];
          op[pos2] = '\0';
          // cout<<a<<endl;
          //cout<<op<<endl;
          insert(a, op);


        }
       scanf("%d",&que);
       getchar();
       while(que--)
       {
           gets(str);
           unsigned int ans1 = 0, ans2 = 0;
           if(str[0] ==  '[')
            {


                int len = strlen(str);
                str[len-1] = '\0';


                ans1 = search_Hasha(str+1);
                if(ans1 != -1)
                cout<<Hasha[ans1].ans<<endl;
                else
                cout<<"what?"<<endl;
            }
          else
          {
              ans2 = search_Hashb(str);
              if(ans2 != -1)
               cout<<Hashb[ans2].que<<endl;
             else
              cout<<"what?"<<endl;
          }




       }


    return 0;
}


map版

#include <iostream>
#include <cstdio>
#include <map>
#include <string>
using namespace std;
map<string,string>mp;
map<string,string>::iterator iter;
string s,t1,t2;
int q;
int main()
{
    int i,j,k,w;
    mp.clear();
    while(getline(cin,s))
    {
        if(s=="@END@")break;
        int pos=s.find(']',0);
        t1=s.substr(1,pos-1);
        t2=s.substr(pos+2,s.length());
        mp[t1]=t2;
        mp[t2]=t1;
        s="";
        t1=t2="";
    }
    scanf("%d ",&q);
    while(q--){
        s=t1="";
        getline(cin,s);
        if(s[0]=='['){
            int pos=s.find(']',0);
            t1=s.substr(1,pos-1);
            if(mp[t1]!=""){
                cout<<mp[t1]<<endl;
            }else
                puts("what?");
        }else{
            if(mp[s]!=""){
                cout<<mp[s]<<endl;
            }else
                puts("what?");
        }
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值