题目链接: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. Scala - 正则表达式匹配例子

    壹Try胜仟言 别忘了 import scala.util.matching._ scala> var s = "a_b_c_d_e"s: String = a_b_c_d_ ...

  2. Mantle 简单教程

    Mantle可以很方便的去书写一个模型层的代码. 使用它可以很方便的去反序列化JSON或者序列化为JSON(需要在MTLModel子类中实现<MTLJSONSerializing>协议) ...

  3. Extjs发票管理系统

    技术特点:Extjs框架,三层架构,Ajax,json 1.仿office2007菜单.介面美观大方,可动态更改皮肤保存至cookie. 2,json数据源与实体类的相互转换. 3.可下载桌面版登录方 ...

  4. float right 换行bug

    Bug产生原因:块里面有换行的元素. CSS: .left{float: left;width: 100px;background: #fff000;} .right{float: right;wid ...

  5. Linux grep和find的区别

    这是两个不同的命令,关于grep:Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expressi ...

  6. composer的create-project安装php框架laravel for mac教程

    通过 Composer 的 create-project 命令安装 Laravel 通过在命令行执行 Composer 的 create-project 命令来安装Laravel: composer ...

  7. insert遭遇阻塞

    insert的阻塞确实不常见,今天碰到了一个,看书又了解一个,整理下.1.多个会话同时向unique字段插入相同的值session1:首先建测试表test,并在字段id上创建一个主键索引(唯一键也可以 ...

  8. LayoutInflater的获取与使用

    在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById().不同点是LayoutInflater是用来找res/layout/下的xml布局文件,并且实例 ...

  9. 根据WSDL生成代理类方式(2)

    运行开发人员工具提示 输入命令行svcutil http://localhost:8080/Test/TestClassPort?wsdl

  10. 加JENKINS的SLAVE节点(LINUX)要注意的事项

    从昨天下午到现在,终于解决了所有的问题.作如下记录要点: 1,从节点只要建好目录,JENKINS的包,MASTER会推送过来. 2,ANT,MAVEN之类的,要配置好环境变量,PROFILE.D .B ...