魔咒词典

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 = ;
  9. bool Hash1[MAX];
  10. bool Hash2[MAX];
  11. int num1[MAX],num2[MAX];
  12. int val1[MAX],val2[MAX];
  13. char xx1[][];int xlen1,cur;
  14. char xx2[][];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 = ;
  50. unsigned int x = ;
  51. while (*str)
  52. {
  53. hash = (hash << ) + (*str++);
  54. if ((x = hash & 0xF0000000L) != )
  55. {
  56. hash ^= (x >> );
  57. hash &= ~x;
  58. }
  59. }
  60. return (hash & 0x7FFFFFFF);
  61. }
  62. int main()
  63. {
  64. char c[],b[];
  65. int i,j,k,n,m;
  66. while(gets(c))
  67. {
  68. xlen1=-;
  69. xlen2=-;
  70. memset(Hash1,false,sizeof(Hash1));
  71. memset(Hash2,false,sizeof(Hash2));
  72. memset(num1,-,sizeof(num1));
  73. memset(num2,-,sizeof(num2));
  74.  
  75. while(strcmp(c,"@END@")!=)
  76. {
  77. n=strlen(c);
  78. for(i=,j=;i<n;i++)
  79. {
  80. b[j++]=c[i];
  81. if(c[i]==']')
  82. {
  83. b[--j]='\0';
  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+);
  92. xlen2++;
  93. Insert(k,Hash2,num2,val2,xlen2);
  94. strcpy(xx2[xlen2],c+i+);
  95. gets(c);
  96. }
  97. scanf("%d",&m);
  98. getchar();
  99. while(m--)
  100. {
  101. gets(c);
  102. n=strlen(c);
  103. if(c[]=='[')
  104. {
  105. c[n-]='\0';
  106. k=ELFHash(c+);
  107. if( found(k,Hash1,num1,val1) )
  108. printf("%s\n",xx2[cur]);
  109. else printf("what?\n");
  110. }
  111. else
  112. {
  113. k=ELFHash(c);
  114. if( found(k,Hash2,num2,val2) )
  115. printf("%s\n",xx1[cur]);
  116. else printf("what?\n");
  117. }
  118. }
  119. }
  120. return ;
  121. }

hdu 1880 魔咒词典 (字符串哈希)的更多相关文章

  1. HDU 1880 魔咒词典 (字符串hash)

    <题目链接> 题目大意: 就是每个字符串有一个配套的对应字符串,询问的时候,无论输出其中的哪一个字符串,输出另一个,如果不存在这个字符串,直接输出"what?". 解题 ...

  2. hdu 1880 魔咒词典

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1880 魔咒词典 Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有10 ...

  3. HDU 1880 魔咒词典(字符串哈希)

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

  4. HDU - 1880 魔咒词典~哈希入门

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

  5. HDU 1880 魔咒词典 (Hash)

    魔咒词典 Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  6. hdu 1880 魔咒词典(双hash)

    魔咒词典Time Limit: 8000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  7. hdu 1880 魔咒字典

    https://vjudge.net/problem/HDU-1880 题意:略 思路: 一开始就是想到了正确的思路,但是代码写炸了,死活过不了.这题嘛,就是建议一个魔咒与咒语的双向映射.首先用字符串 ...

  8. 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )

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

  9. 魔咒词典(hdu 1880)

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

随机推荐

  1. Qt5学习笔记(消息基础)

    #include "MyWidget.h" #include <QApplication> #include <QEvent> #include <Q ...

  2. cnpm安装过程中提示optional install error: Package require os(darwin) not compatible with your platform(win32)解决方法

    运行cnpm install后,出现 虽然提示不适合Windows,但是问题好像是sass loader出问题的.所以只要执行下面命令即可: 方案一: cnpm rebuild node-sass # ...

  3. Hibernate 查询数据库中的数据

    1.Criteria介绍 Criteria与Session绑定,其生命周期跟随着Session结束而结束,使用Criteria时进行查询时,每次都要于执行时期动态建立物件,并加入各种查询条件,随着Se ...

  4. 将python的代码文件打包成可执行文件

    1.使用pip install Pyinstaller  命令安装 2.使用命令 pyinstaller -F  *.py打包成exe 3.在\dist文件夹下找到exe; 一.pyinstaller ...

  5. orcal创建序列

    CREATE SEQUENCE flowjobseq --序列名INCREMENT BY 1 -- 每次加几个 START WITH 2000 -- 从1开始计数 NOMAXVALUE -- 不设置最 ...

  6. Django环境搭建之hello world

    当我们想用Python来开发一个web应用时,首先要选择一个优秀的web框架,Django是个非常成熟的web开发框架,网上具有丰富的文档和学习资料,所以选择Django框架来入门web开发是个不错的 ...

  7. 最新版IntelliJ IDEA2019.1破解教程(2019.04.08更新)

    [原文链接]:https://www.tecchen.xyz/idea-crack.html 我的个人博客:https://www.tecchen.xyz,博文同步发布到博客园. 由于精力有限,对文章 ...

  8. np.array()和np.mat()区别

    1. 生成数组所需格式不同 mat可以从字符串或列表中生成:array只能从列表中生成 2. 生成的数组计算方式不同 array生成数组,用np.dot()表示矩阵乘积,(*)号或np.multipl ...

  9. [转] String to Date conversion in hive - 在 Hive 中各种字符串转换成日期格式

    [From] http://bigdataprogrammers.com/string-date-conversion-hive/ Please refer below table to conver ...

  10. C#控制台画图形

    static void Main(string[] args) { //九九乘法 Console.WriteLine("九九乘法口诀"); ; i <= ; i++) { ; ...