• hdu 1880 魔咒词典 (字符串哈希)


    魔咒词典

    Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 9381    Accepted Submission(s): 2405


    Problem Description
    哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。

    给你一部魔咒词典。当哈利听到一个魔咒时,你的程序必须告诉他那个魔咒的功能;当哈利需要某个功能但不知道该用什么魔咒时,你的程序要替他找到相应的魔咒。如果他要的魔咒不在词典中,就输出“what?”
     
    Input
    首先列出词典中不超过100000条不同的魔咒词条,每条格式为:

    [魔咒] 对应功能

    其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。
    词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。
     
    Output
    每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”
     
    Sample Input
    [expelliarmus] the disarming charm [rictusempra] send a jet of silver light to hit the enemy [tarantallegra] control the movement of one's legs [serpensortia] shoot a snake out of the end of one's wand [lumos] light the wand [obliviate] the memory charm [expecto patronum] send a Patronus to the dementors [accio] the summoning charm @END@ 4 [lumos] the summoning charm [arha] take me to the sky
     
    Sample Output
     
    light the wand
    accio
    what?
    what?
     
    Author
    ZJU
     
    Source
     
     
     
     
      1 #include<iostream>
      2 #include<stdio.h>
      3 #include<cstring>
      4 #include<cstdlib>
      5 #include<algorithm>
      6 using namespace std;
      7 
      8 const int MAX = 100007;
      9 bool Hash1[MAX];
     10 bool Hash2[MAX];
     11 int num1[MAX],num2[MAX];
     12 int val1[MAX],val2[MAX];
     13 char xx1[100002][22];int xlen1,cur;
     14 char xx2[100002][82];int xlen2;
     15 
     16 void Insert(int x,bool *hash,int *num,int *val,int len)
     17 {
     18     int k=x%MAX;
     19     while(hash[k]==true && num[k]!=x)
     20     {
     21         k++;
     22         if(k==MAX) k=k-MAX;
     23     }
     24     if(hash[k]==false)
     25     {
     26         hash[k]=true;
     27         num[k]=x;
     28         val[k]=len;
     29     }
     30 }
     31 bool found(int x,bool *hash,int *num,int *val)
     32 {
     33     int k=x%MAX;
     34     while(hash[k]==true && num[k]!=x)
     35     {
     36         k++;
     37         if(k==MAX) k=k-MAX;
     38     }
     39     if(num[k]==x)
     40     {
     41         cur=val[k];
     42         return true;
     43     }
     44     return false;
     45 }
     46 // ELF Hash Function
     47 unsigned int ELFHash(char *str)
     48 {
     49     unsigned int hash = 0;
     50     unsigned int x = 0;
     51     while (*str)
     52     {
     53         hash = (hash << 4) + (*str++);
     54         if ((x = hash & 0xF0000000L) != 0)
     55         {
     56             hash ^= (x >> 24);
     57             hash &= ~x;
     58         }
     59     }
     60     return (hash & 0x7FFFFFFF);
     61 }
     62 int main()
     63 {
     64     char c[150],b[20];
     65     int i,j,k,n,m;
     66     while(gets(c))
     67     {
     68         xlen1=-1;
     69         xlen2=-1;
     70         memset(Hash1,false,sizeof(Hash1));
     71         memset(Hash2,false,sizeof(Hash2));
     72         memset(num1,-1,sizeof(num1));
     73         memset(num2,-1,sizeof(num2));
     74 
     75         while(strcmp(c,"@END@")!=0)
     76         {
     77             n=strlen(c);
     78             for(i=1,j=0;i<n;i++)
     79             {
     80                 b[j++]=c[i];
     81                 if(c[i]==']')
     82                 {
     83                     b[--j]='';
     84                     k=ELFHash(b);
     85                     xlen1++;
     86                     Insert(k,Hash1,num1,val1,xlen1);
     87                     strcpy(xx1[xlen1],b);
     88                     break;
     89                 }
     90             }
     91             k=ELFHash(c+i+2);
     92             xlen2++;
     93             Insert(k,Hash2,num2,val2,xlen2);
     94             strcpy(xx2[xlen2],c+i+2);
     95             gets(c);
     96         }
     97         scanf("%d",&m);
     98         getchar();
     99         while(m--)
    100         {
    101             gets(c);
    102             n=strlen(c);
    103             if(c[0]=='[')
    104             {
    105                 c[n-1]='';
    106                 k=ELFHash(c+1);
    107                 if( found(k,Hash1,num1,val1) )
    108                     printf("%s
    ",xx2[cur]);
    109                 else printf("what?
    ");
    110             }
    111             else
    112             {
    113                 k=ELFHash(c);
    114                 if( found(k,Hash2,num2,val2) )
    115                     printf("%s
    ",xx1[cur]);
    116                 else printf("what?
    ");
    117             }
    118         }
    119     }
    120     return 0;
    121 }
  • 相关阅读:
    let 和 const 命令
    字符串和数组之间相互转换
    angularjs 添加拦截器
    js判断数组是否包含指定元素的方法
    angularjs ng-include
    requirejs学习 关于requirejs的一些参数问题
    ngDialog 点击窗口以外不允许关闭弹窗
    jQuery Jcrop 图像裁剪
    Javascript模块化编程(三):require.js的用法
    angular.bootstrap手动加载angularjs模块
  • 原文地址:https://www.cnblogs.com/tom987690183/p/3597143.html
Copyright © 2020-2023  润新知