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;
}

  

POJ2503 Babelfish map或者hash_map的更多相关文章

  1. POJ2503——Babelfish(map映射+string字符串)

    Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...

  2. map,hash_map, hash_table, 红黑树 的原理和使用

    在刷算法题的时候总是碰到好多题,号称可以用hash table来解题.然后就蒙圈了. 1.首先,map和hash_map的区别和使用: (1)map底层用红黑树实现,hash_map底层用hash_t ...

  3. Map和hash_map

    map和hash_map 今天在写拼流的程序时碰到一个问题,要根据流的四元组的结构信息映射到该流的数据.也就是我在网络数据包拼接的过程中,要根据包的地址和端口信息,对应到其对应的一个流的数据上去,把端 ...

  4. STL中map与hash_map容器的选择收藏

    这篇文章来自我今天碰到的一个问题,一个朋友问我使用map和hash_map的效率问题,虽然我也了解一些,但是我不敢直接告诉朋友,因为我怕我说错了,通过我查询一些帖子,我这里做一个总结!内容分别来自al ...

  5. map与hash_map使用与对比

    #include <iostream> #include <functional> #include <map> #include <ext/hash_map ...

  6. STL 中的map 与 hash_map的理解

    可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...

  7. STL中的map和hash_map

    以下全部copy于:http://blog.chinaunix.net/uid-26548237-id-3800125.html 在网上看到有关STL中hash_map的文章,以及一些其他关于STL ...

  8. map、hash_map、unordered_map 的思考

    #include <map> map<string,int> dict; map是基于红黑树实现的,可以快速查找一个元素是否存在,是关系型容器,能够表达两个数据之间的映射关系. ...

  9. map vs hash_map

    1. map, multimap, set, multiset g++ 中 map, multimap, set, multiset 由红黑树实现 map: bits/stl_map.h multim ...

随机推荐

  1. 19Spring返回通知&异常通知&环绕通知

    在前置通知和后置通知的基础上加上返回通知&异常通知&环绕通知 代码: package com.cn.spring.aop.impl; //加减乘除的接口类 public interfa ...

  2. MySQL-----一对一

    一对一: 用户表和博客表 用户表(userinfo): 用户id 用户名 1 George 2 root 3 Bruce 4 Catherine 博客表: 博客id 博客名 用户id(FK + 唯一) ...

  3. linux上uwsgi+nginx+django发布项目

    在发布项目前首先将部署环境进行搭建,尤其是依赖包一定需要提前安装. 一.虚拟环境的搭建 1.建议在linux下新建一个虚拟环境,这样有独立干净的环境. mkvirtualenv -p python3 ...

  4. crm项目之stark组件前戏(二)

    stark组件的设计主要来源于django中admin的功能,在django admin中只需要将模型表进行注册,就可以在页面对该表进行curd的动作,那么django admin是如何做的呢? 在d ...

  5. Hadoop安装与配置

    Hadoop介绍 上面是官方介绍,翻一下来总结一句话就是:Hadoop是一个高可用,用于分布式处理大规模计算的工具. Hadoop1.2 下载 . Hadoop1.2 安装 1. 安装jDK 2. 配 ...

  6. 集训第四周(高效算法设计)P题 (构造题)

    Description   There are N<tex2html_verbatim_mark> marbles, which are labeled 1, 2,..., N<te ...

  7. 安装 asp.net core 出错

    I received the same error message on a fresh Windows 10 install, with a fresh Visual Studio 2015 ins ...

  8. iPhone安装ipa的方法(iTunes,PP助手)

    1,通过iTunes: 将手机与电脑通过数据线连接,打开电脑中的iTunes,将ipa文件添加到资料库(ipa文件是iTunes能够识别的文件),方式如下图,然后安装,同步即可. 2,通过PP助手: ...

  9. 第五章、 Linux 常用網路指令

    http://linux.vbird.org/linux_server/0140networkcommand.php     第五章. Linux 常用網路指令 切換解析度為 800x600 最近更新 ...

  10. Automation 的 Wait 工具

    public static WebDriverWait createWait(WebDriver driver) { return new WebDriverWait(driver, Environm ...