poj 2503 字符串hash
题目链接: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的更多相关文章
- POJ 1200 字符串HASH
题目链接:http://poj.org/problem?id=1200 题意:给定一个字符串,字符串只有NC个不同的字符,问这个字符串所有长度为N的子串有多少个不相同. 思路:字符串HASH,因为只有 ...
- poj 1200字符串hash
题意:给出不同字符个数和子串长度,判断有多少个不同的子串 思路:字符串hash. 用字符串函数+map为什么会超时呢?? 代码: #include <iostream> #include ...
- poj 2503(字符串)
http://poj.org/problem?id=2503 题意:就是翻译,给你一个字典,然后再查找单词,找得到的就输出单词,找不到的输出eh,用Map水题一个,但这个题有点意思的就是输入的问题 # ...
- 字符串hash + 二分答案 - 求最长公共子串 --- poj 2774
Long Long Message Problem's Link:http://poj.org/problem?id=2774 Mean: 求两个字符串的最长公共子串的长度. analyse: 前面在 ...
- POJ 3865 - Database 字符串hash
[题意] 给一个字符串组成的矩阵,规模为n*m(n<=10000,m<=10),如果某两列中存在两行完全相同,则输出NO和两行行号和两列列号,否则输出YES [题解] 因为m很小,所以对每 ...
- POJ 1743 Musical Theme (字符串HASH+二分)
Musical Theme Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 15900 Accepted: 5494 De ...
- poj 3461 字符串单串匹配--KMP或者字符串HASH
http://poj.org/problem?id=3461 先来一发KMP算法: #include <cstdio> #include <cstring> #include ...
- 字符串hash - POJ 3461 Oulipo
Oulipo Problem's Link ---------------------------------------------------------------------------- M ...
- Palindrome POJ - 3974 (字符串hash+二分)
Andy the smart computer science student was attending an algorithms class when the professor asked t ...
随机推荐
- git 401 错误
错误信息:error: The requested URL returned error: 401 Unauthorized while accessing https://git.oschina.n ...
- JavaScript学习总结【11】、JS 运动
动画效果在网站中是一种非常常见的交互式体验效果,比如侧边栏分享.图片淡入淡出,我们把这种动画效果就叫做运动,也就是让物体动起来.如果想让一个物体动起来,无非就是改变它的速度,也就是改变属性值,比如 l ...
- PHP输出图片文件,实现浏览器缓存机制
$ETag = '"'.md5($file).'"'; if(env('HTTP_IF_NONE_MATCH') === $ETag){ header ( 'ETag: '.$ET ...
- redis的安装-windows和linux
windows 下载地址:http://code.google.com/p/servicestack/wiki/RedisWindowsDownload 下载解压到D盘下: 进到该目录下,有下列文件: ...
- keilkill.bat
一.批处理文件 批处理文件是无格式的文本文件,它包含一条或多条命令.它的文件扩展名为 .bat 或 .cmd.在命令提示下键入批处理文件的名称,或者双击该批处理文件,系统就会调用Cmd.exe按照该文 ...
- 解决方案:安装wordpress出现500 Internal Server Error
做一个资讯站点的时候遇到一个wordpress不知道算不算常见的问题:程序安装的时候提示500 Internal Server Error 那么最终百度谷歌找到以下解决方案: 安装新版本wordpre ...
- CTSC && APIO 总结
先说CTSC吧,第一试其实不难但是下意识觉得CTSC不就只能骗分吗,然后就全上暴力了.然而第二题再一次看漏了条件,即答案总和小于等于1e6.第三题现在回想起来要不然就是没有思考,要不然就是没学过数学, ...
- awsomeplayer结构认识
把这个搞明白,算是顿悟的一个真实例子.怎么也搞不懂的架构,突然就想明白了.不过这其实是一个思维的过程. 当然如果你想明白这些东西,至少要非常清楚一个概念:接口. 我只是一个半路出家的开发者,我真正明白 ...
- Altium Designer10 如何导出Gerber文件
版本:AD10.818 目的:Gerber文件导出备忘 http://blog.sina.com.cn/s/blog_9b9a51990100zyyv.html 目录: Step1:设置原点 Step ...
- 带文字的ImageButton[自定义]
今天Android项目中遇到一个ImageButton控件上面要显示文字,无奈自定义了一个ImageButton,继承自ImageButton.其实就是override这个控件的onDraw(Canv ...