题目链接:http://poj.org/problem?id=2503

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm> using namespace std; const int maxn = ;
const int HASH = ; int head[HASH],next[maxn];
char s1[maxn][],s2[maxn][];
int n; int hash(char *s)
{
int ret = ;
int seed = ;
while(*s)
{
ret = ret * seed + *(s++);
}
return (ret & 0x7fffffff) % HASH;
} int search(char *s)
{
int i;
int id = hash(s);
for(i=head[id]; i!=-; i=next[i])
{
if(strcmp(s,s2[i]) == )
break;
}
return i;
} void input()
{
memset(head,-,sizeof(head));
n = ;
char ch[];
gets(ch);
while(ch[] != '\0')
{
int i=,j;
for(i=,j=; ch[i]!=' '; i++,j++)
s1[n][j] = ch[i];
s1[n][j] = '\0';
i++;
for(j=; ch[i]!='\0'; i++,j++)
s2[n][j] = ch[i];
s2[n][j] = '\0'; int id = hash(s2[n]);
next[n] = head[id];
head[id] = n;
n++;
gets(ch);
}
} void solve()
{
char ch[];
while(scanf("%s",ch) == )
{
int id = search(ch);
if(id == -)
{
printf("eh\n");
}
else
{
printf("%s\n",s1[id]);
}
}
}
int main()
{
// freopen("E:\\acm\\input.txt","r",stdin);
input();
solve();
}

poj 2503 字符串hash的更多相关文章

  1. POJ 1200 字符串HASH

    题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...

  2. poj 1200字符串hash

    题意:给出不同字符个数和子串长度,判断有多少个不同的子串 思路:字符串hash. 用字符串函数+map为什么会超时呢?? 代码: #include <iostream> #include ...

  3. poj 2503(字符串)

    http://poj.org/problem?id=2503 题意:就是翻译,给你一个字典,然后再查找单词,找得到的就输出单词,找不到的输出eh,用Map水题一个,但这个题有点意思的就是输入的问题 # ...

  4. 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774

    Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...

  5. POJ 3865 - Database 字符串hash

    [题意] 给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES [题解] 因为m很小,所以对每 ...

  6. POJ 1743 Musical Theme (字符串HASH+二分)

    Musical Theme Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 15900   Accepted: 5494 De ...

  7. poj 3461 字符串单串匹配--KMP或者字符串HASH

    http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...

  8. 字符串hash - POJ 3461 Oulipo

    Oulipo Problem's Link ---------------------------------------------------------------------------- M ...

  9. Palindrome POJ - 3974 (字符串hash+二分)

    Andy the smart computer science student was attending an algorithms class when the professor asked t ...

随机推荐

  1. phpredis

    安装php的redis扩展: http://pecl.php.net/package/redis 也可以用PHP直接连redis: http://www.cnblogs.com/kudosharry/ ...

  2. (四)跟我一起玩Linux网络服务:DHCP服务配置之中继代理

    继第三部分的DHCP服务器的设置成功,我们来做一个中继代理服务器的配置吧. 我们的虚拟机结构如图: 具体参考: (一)跟我一起玩Linux网络服务:DNS服务——BIND(/etc/named.con ...

  3. a标签使用

    1.发起邮件 注意:如果mailto后面同时有多个参数的话,第一个参数必须以“?”开头,后面的参数每一个都以“&”分隔. <a href="mailto:281345774@q ...

  4. 字符串长度计算、截取、url参数获取、计算百分比、时间戳格式化

    1.中英混合文字字符截取 //中文长度截取计算,可取中英混合,个数向上取整,精确度1个英文字符误差,一个英文算一个字符,一个汉字算一个字符. //sub("中文zlsd",1) - ...

  5. 结构型模式(Structural patterns)->外观模式(Facade Pattern)

    动机(Motivate): 在软件开发系统中,客户程序经常会与复杂系统的内部子系统之间产生耦合,而导致客户程序随着子系统的变化而变化.那么如何简化客户程序与子系统之间的交互接口?如何将复杂系统的内部子 ...

  6. mongodb3.2系统性学习——2、write concern mongodb 写安全机制

    为了尊重作者原文章位置:http://kyfxbl.iteye.com/blog/1952941 首先讲一下mongodb 的写操作过程: mongodb有一个write concern的设置,作用是 ...

  7. maya2105 - windows8 - numpy/scipy

    To compile numpy, create a site.cfg file in numpy's source directory with the following or similar c ...

  8. 利用Jquery实现http长连接(LongPoll)

    参考:http://www.cnblogs.com/vagerent/archive/2010/02/05/1664450.html PS:为了满足 某些需要 实时请求的业务(PS:例如聊天室),我们 ...

  9. C#中的委托事件的分析

    推荐:http://www.cnblogs.com/SkySoot/archive/2012/04/05/2433639.html 委托和事件在 .NET Framework 中的应用非常广泛,然而, ...

  10. 【网络流24题】No.8 机器人路径规划问题

    [题意] 机器人 Rob 可在一个树状路径上自由移动. 给定树状路径 T 上的起点 s 和终点 t, 机器人 Rob 要从 s 运动到 t. 树状路径 T 上有若干可移动的障碍物. 由于路径狭窄, 任 ...