题目链接: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. epoll和select区别

    先说下本文框架,先是问题引出,然后概括两个机制的区别和联系,最后介绍每个接口的用法 一.问题引出 联系区别 问题的引出,当需要读两个以上的I/O的时候,如果使用阻塞式的I/O,那么可能长时间的阻塞在一 ...

  2. ubuntu下的supervisor启动express失败问题

    ubuntu下apt-get install nodejs后的启动命令是nodejs,而不同于windows下的node 所以我在supervisor启动express的时候出现了问题 提示如下: / ...

  3. Winform与WPF对话框(MessageBox, Dialog)之比较

    Winform:使用System.Windows.Forms命名空间中相应控件; WPF则调用Microsoft.Win32. MessageBox: // WinForm private void ...

  4. MDK建立STM32F103*开发模板

    一.整体流程 1.获取ST库--STM32F10x_StdPeriph_Lib_V3.5.0 2.新建文件夹并加载文件 3.新建工程 4.给工程添加组 5.设置"Target Option& ...

  5. 解决vsftpd 2.2.2读取目录列表失败的问题

    该错误是由iptables的配置引起的,临时的解决方法是执行如下命令: [root@localhost soft]# modprobe ip_nat_ftp 再次登陆列表正常啦! 但当你重新启动服务器 ...

  6. qt5 基础知识

    QWidget wQLineEdit edit; edit.show(); //如果没有这句,编辑框edit将会显示在父窗口的左上角edit.setParent(&w); //以w为父窗口并显 ...

  7. Swift2.0新特性

    随着刚刚结束的 WWDC 2015 苹果发布了一系列更新,这其中就包括了令人振奋的 Swift 2.0. 这是对之前语言特性的一次大幅的更新,加入了很多实用和方便的元素,下面我们就一起来看看这次更新都 ...

  8. 设置UITextField的placeholder的颜色

    [textField setValue:[UIColor redColor] forKeyPath:@"_placeholderLabel.textColor"];

  9. sublime部署开发环境

    安装nodejs 去官网下载安装,暂时不建议安装5.0以上版本. 命令行运行以下命令查看是否安装成功: 全局安装gulp $ npm install -g gulp 全局安装requirejs 压缩资 ...

  10. DDD领域驱动设计和实践(转载)

    -->目录导航 一. DDD领域驱动设计介绍 1. 什么是领域驱动设计(DDD) 2. 领域驱动设计的特点 3. 如果不使用DDD? 4. 领域驱动设计的分层架构和构成要素 5. 事务脚本和领域 ...