HDU - 1880 字符串哈希
2017-04-09 11:08
357 查看
题意:
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1880通过咒语查找功能,通过功能查找咒语。
思路:
字符串哈希模板题代码:
#include <bits/stdc++.h> using namespace std; const int MAXN = 210000; const int MOD = 100007; struct Node { Node* nxt; // 后继指针 char str[100]; // 保存字符串 }; struct StrHash { Node Hash[MAXN], *head[MOD], *cur; // head数组记录同一哈希值的字符串的地址链表 void init() { cur = Hash; memset(head, 0, sizeof(head)); } unsigned int BKDHash(char* s) { // 计算哈希值 unsigned int seed = 131, res = 0; while (*s) res = res * seed + *s++; return (res & 0x7FFFFFFF) % MOD; } int getId(char* s) { // 从0开始依次将字符串插入hash数组,并计算哈希值,维护head链表 int code = BKDHash(s); Node* ptr = head[code]; while (ptr) { if (strcmp(ptr->str, s) == 0) return ptr - Hash; else ptr = ptr->nxt; } strcpy(cur->str, s); cur->nxt = head[code]; head[code] = cur++; return cur - Hash - 1; } int Find(char* s) { // 通过s的哈希值,找到head链表首部。然后遍历链表找到字符串在hash数组中的位置 int code = BKDHash(s); Node* ptr = head[code]; while (ptr) { if (strcmp(ptr->str, s) == 0) return ptr - Hash; else ptr = ptr->nxt; } return -1; } } strhash; char s[100], *p; int main() { //freopen("in.txt", "r", stdin); strhash.init(); while (scanf("%s", s), s[0] != '@') { strhash.getId(s); getchar(); gets(s); strhash.getId(s); } int q; scanf("%d", &q); getchar(); while (q--) { gets(s); int id = strhash.Find(s); if (id == -1) puts("what?"); else { p = strhash.Hash[id ^ 1].str; // 对本题来说,两个相关的字符串互为奇偶 if (p[0] != '[') puts(p); else { p++; while (*p != ']') putchar(*p++ ); puts(""); } } } return 0; }
相关文章推荐
- HDU 1880 魔咒词典(字符串哈希)
- hdu 1880 魔咒词典 (字符串哈希)
- hdu1880魔咒词典 字符串哈希
- HDU 1880 字符串hash 入门题
- hdu 1880 魔咒词典--字符串简单查找
- [HDU 4080] Stammering Aliens (字符串哈希+二分)
- hdu 1880 字符串hash
- hdu 3973 AC's String 字符串哈希处理 查询子串是否是模式串 可以修改
- HDU 4622 Reincarnation (区间不相同子串个数:字符串哈希 | 后缀数组 | 后缀自动机)
- HDU 4821 String(字符串哈希)
- HDU 4821 String 字符串哈希
- hdu 2736 Surprising Strings(类似哈希,字符串处理)
- hdu(1880):魔咒词典——字符串hash的应用
- [HDU 4821] String (字符串哈希)
- HDU 5510 Bazinga(技巧 + 字符串哈希)
- hdu 1228 A + B 详细题解 字符串/哈希
- HDU 1880 字符串hash 入门题
- HDU 4436 str2int 后缀数组 字符串哈希 前缀和
- HDU 3973 AC's String 字符串哈希
- HDU 1800——Flying to the Mars——————【字符串哈希】