POJ2503 这是一道水题,用Map轻松AC。

不过,可以拿来测一下字符串散列, 毕竟,很多情况下map无法解决的映射问题需要用到字符串散列。

自己生成一个质数, 随便搞一下。

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string.h>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int maxn=1e+6;
const int P=999983;
unsigned int base[100];
int dic[maxn];
char eng[100010][11];
inline void init_hash(char* s,int h[])
{
int l=strlen(s);
h[0]=0;
for(int i=1;i<=l;++i)
{
h[i]=(h[i-1]*26+s[i-1])%P;
}
base[0]=0;
for(int i=1;i<=l;i++)
base[i]=(base[i-1]*26)%P;
} inline unsigned int string_hash(char* s,int h[], int l, int r)//[l,r)左闭右开
{
init_hash(s,h);
return (h[r]-h[l]*base[r-1])%P;
}
int ha[maxn];
int main()
{freopen("t.txt","r",stdin);
//ios::sync_with_stdio(false);
memset(dic,0,sizeof(dic));
char lan[11];
int ditlen=1,tot=0;
eng[0][0]='e';
eng[0][1]='h';
eng[0][3]='\0';
while(true)
{
char t=getchar();
if(t=='\n')break;
int len=1;
eng[ditlen][0]=t;
while(true)
{
t=getchar();
if(t==' '){eng[ditlen][len++]='\0';ditlen++;break;}
eng[ditlen][len++]=t;
}
int lent=0;
while(true)
{
t=getchar();
if(t=='\n'){lan[lent++]='\0';break;}
lan[lent++]=t;
}
int leng=strlen(lan);
int has=string_hash(lan,ha,0,leng);
while(has<0)has+=P;
dic[has]=ditlen-1;
}
while(scanf("%s",lan)!=EOF)
{
int has=string_hash(lan,ha,0,strlen(lan));
while(has<0)has+=P;
has=dic[has];
for(int i=0;;i++)
{
if(eng[has][i]=='\0')break;
printf("%c",eng[has][i]);
}
printf("\n");
}
return 0;
}

  

最新文章

  1. JMeter常见问题集合
  2. 动画在webapp中的现状
  3. NDK开发历程(一):android native code的调试方法
  4. [源码]String StringBuffer StringBudlider(2)StringBuffer StringBuilder源码分析
  5. JSP中&lt;base href=&quot;&lt;%=basePath%&gt;&quot;&gt;的作用
  6. Ios(ipad iphone) 支持字体一览
  7. Jersey(1.19.1) - Conditional GETs and Returning 304 (Not Modified) Responses
  8. 基础学习总结(三)--文本、SD卡数据读写
  9. logstash 处理tomcat access报ArgumentError: comparison of String with 5 failed
  10. 动态改变ComboBox下拉框的宽度
  11. Hibernate5-课程笔记4
  12. Tomcat系列(8)——Tomcat运行模式连接数和线程池
  13. Echatrs 中PIE饼图中间位置怎么显示总数值?
  14. Jenkins+PowerShell持续集成环境搭建(五)SSRS项目
  15. 洗礼灵魂,修炼python(12)--python关键词,包
  16. 转载 【.NET基础】--委托、事件、线程(3)
  17. 谁说前端不需要懂-Nginx反向代理与负载均衡
  18. R语言编程艺术#02#矩阵(matrix)和数组(array)
  19. Greenplum5.16.0 安装教程
  20. Maven 进阶

热门文章

  1. UVa 11998 破碎的键盘(数组实现链表)
  2. Qt笔记——2.编写多窗口程序
  3. Leetcode 214.最短回文串
  4. 数据结构-B+树
  5. MVC系统学习7—Action的选择过程
  6. noip模拟赛 轮换
  7. zoj2853 Evolution
  8. 微信小程序 wafer2框架摘要
  9. Thread.run方法是同步方法
  10. [Maid] Write Tasks in Markdown with Maid