zoukankan      html  css  js  c++  java
  • HDU-魔咒词典(字符串hash)

    魔咒词典

    TimeLimit: 8000/5000 MS (Java/Others)  MemoryLimit: 32768/32768 K (Java/Others)
    64-bit integer IO format:%I64d
     
    Problem Description
    哈利波特在魔法学校的必修课之一就是学习魔咒。据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助。 

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

    [魔咒] 对应功能 

    其中“魔咒”和“对应功能”分别为长度不超过20和80的字符串,字符串中保证不包含字符“[”和“]”,且“]”和后面的字符串之间有且仅有一个空格。词典最后一行以“@END@”结束,这一行不属于词典中的词条。 
    词典之后的一行包含正整数N(<=1000),随后是N个测试用例。每个测试用例占一行,或者给出“[魔咒]”,或者给出“对应功能”。
    Output
    每个测试用例的输出占一行,输出魔咒对应的功能,或者功能对应的魔咒。如果魔咒不在词典中,就输出“what?”
    SampleInput
    [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
    SampleOutput
    light the wand
    accio
    what?
    what?

    题意:这题的话,就是通过前面的字符串查找后面的字符串,或者是通过后面的字符串查找前面的字符串。这里我用的字符串hash来写,由于这道题比较卡内存,因此在分配内存的时候选择的是手动分配内存,当然你可以提前分配好,但是
    尽量不要超太多,不然会被卡掉。处理hash冲突时,这里通过链表的方式处理hash的,跟链式前向星差不多,相信有学过这个就很容易理解这种写法。
    这里的ELFHhash函数可以参考这个博客https://blog.csdn.net/weixin_39002938/article/details/77855824,这里讲得算是蛮清楚的了。
      1 #include <cstdio>
      2 #include <cstring>
      3 #include <iostream>
      4 #include <algorithm>
      5 
      6 
      7 using namespace std;
      8 const int maxn = 1e5 + 7;///范围
      9 
     10 struct Hash_map
     11 {
     12 
     13     static const int maxn = 2e5 + 7;///字符串的最大数量
     14     static const int MAXINT = 0x7FFFFFFF;
     15 
     16     int first[maxn], sign;
     17 
     18     struct Edge
     19     {
     20         int to, next;
     21         char *str, *ttr;
     22     } edge[maxn];
     23 
     24     inline void init()
     25     {
     26         memset(first, -1, sizeof(first));
     27         sign = 0;
     28     }
     29 
     30     inline int get_hash(char *str)
     31     {
     32         unsigned  long long h=0;
     33         while(*str)
     34         {
     35             h=(h<<4)+(*str++);
     36             long long g=h&0Xf0000000L;
     37             if(g)
     38                 h^=g>>24;
     39             h&=~g;
     40         }
     41         return h&MAXINT;
     42     }
     43 
     44     inline void add_edge(int u, char *str, char *ttr)
     45     {
     46         int lens = strlen(str), lent = strlen(ttr);
     47         edge[sign].str = new char[lens + 1];
     48         edge[sign].ttr = new char[lent + 1];
     49         strcpy(edge[sign].str, str);
     50         strcpy(edge[sign].ttr, ttr);
     51         edge[sign].next = first[u];
     52         first[u] = sign ++;
     53     }
     54 
     55     inline void Insert(char *str, char *ttr)
     56     {
     57         int key = get_hash(str) % (maxn);
     58         add_edge(key, str, ttr);
     59     }
     60 
     61     inline bool Find(char *str)
     62     {
     63         int key = get_hash(str) % (maxn);
     64         for(int i = first[key]; ~i; i = edge[i].next)
     65         {
     66             if(!strcmp(str, edge[i].str))
     67             {
     68                 puts(edge[i].ttr);
     69                 return 1;
     70             }
     71         }
     72         puts("what?");
     73         return 0;
     74     }
     75 } T1;
     76 
     77 char str[105], ttr[105], ktr[105];
     78 int n;
     79 int main()
     80 {
     81     T1.init();
     82 
     83     while(scanf("%s",str)&&strcmp(str,"@END@")!=0)
     84     {
     85         int len=strlen(str);
     86         for(int i=0; i<len-1; i++)
     87             str[i]=str[i+1];
     88         str[len-2]='';
     89         getchar();
     90         gets(ttr);
     91         T1.Insert(str,ttr);
     92         T1.Insert(ttr,str);
     93 
     94     }
     95     scanf("%d",&n);
     96     getchar();
     97     while(n--)
     98     {
     99         gets(ktr);
    100         if(ktr[0]=='[')
    101         {
    102             int len=strlen(ktr);
    103             for(int i=0; i<len-1; i++)
    104                 ktr[i]=ktr[i+1];
    105             ktr[len-2]='';
    106 
    107         }
    108         T1.Find(ktr);
    109     }
    110     return 0;
    111 
    112 
    113 }
    View Code
     
  • 相关阅读:
    姐姐的vue(1)
    LeetCode 64. Minimum Path Sum 20170515
    LeetCode 56. 56. Merge Intervals 20170508
    LeetCode 26. Remove Duplicates from Sorted Array
    LeetCode 24. Swap Nodes in Pairs 20170424
    LeetCode 19. Remove Nth Node From End of List 20170417
    LeetCode No.9 Palindrome Number 20170410
    LeetCode No.8. String to Integer (atoi) 2017/4/10(补上一周)
    LeetCode No.7 Reverse Integer 2017/3/27
    LeetCode No.4 Median of Two Sorted Arrays 20170319
  • 原文地址:https://www.cnblogs.com/yuanlinghao/p/10439370.html
Copyright © 2011-2022 走看看