字符串hash问题
来源:互联网 发布:pmp网络课程 编辑:程序博客网 时间:2024/06/06 01:12
一些介绍字符串hash的文章:
字符串哈希函数
String
题目传送:HDU - 4821 - String
13年长春现场赛的字符串水题。。
不过没有接触过这类题,所以不会做。。
所谓字符串hash,通常是把一个字符串映射为一个整数。
我这里采用的是BKDRHash函数。稳定性最好的一个字符串hash函数。
AC代码:
#include <map>#include <set>#include <list>#include <cmath>#include <deque>#include <queue>#include <stack>#include <bitset>#include <cctype>#include <cstdio>#include <string>#include <vector>#include <complex>#include <cstdlib>#include <cstring>#include <fstream>#include <sstream>#include <utility>#include <iostream>#include <algorithm>#include <functional>#define LL long long#define INF 0x7fffffffusing namespace std;#define ULL unsigned long longconst int maxn = 100005;int m, l;char s[maxn];ULL base[maxn];//base[i]存i个seed相乘的值ULL Hash[maxn];//Hash[i]存后缀i的Hash值ULL seed = 31;//用于映射出去,为了减少冲突,通常取31,131等等这样的质数map<ULL, int> mp;//用于判重int main() { base[0] = 1; for(int i = 1; i <= maxn; i ++) base[i] = base[i - 1] * seed; while(scanf("%d %d", &m, &l) != EOF) { scanf("%s", s); int len = strlen(s); Hash[len] = 0; for(int i = len - 1; i >= 0; i --) {//字符串hash Hash[i] = Hash[i + 1] * seed + s[i] - 'a' + 1; } int ans = 0; for(int i = 0; i < l && i + m * l <= len; i ++) { mp.clear(); for(int j = i; j < i + m * l; j += l) { mp[Hash[j] - Hash[j + l] * base[l]] ++; } if(mp.size() == m) ans ++; for(int j = i + m * l; j + l <= len; j += l) { ULL tmp = Hash[j - m * l] - Hash[j - (m - 1) * l] * base[l]; mp[tmp] --; if(mp[tmp] == 0) mp.erase(tmp); tmp = Hash[j] - Hash[j + l] * base[l]; mp[tmp] ++; if(mp.size() == m) ans ++; } } printf("%d\n", ans); } return 0;}
0 0
- 字符串hash问题
- 字符串hash
- 字符串HASH
- 字符串Hash
- 字符串hash
- 字符串hash
- 字符串hash
- 字符串Hash
- 字符串hash
- 字符串hash
- 字符串 HASH
- 字符串Hash
- 字符串hash
- 字符串hash
- 字符串hash
- 字符串hash
- 字符串hash
- 字符串hash
- 循环赛日程表
- 保存dotNetCharting for WinForm 控件的生成的图表
- 单点更新区间求和 hdu1166 地兵布阵
- VTK6.2&6.3+VS2013+QT5编译配置
- Instruments的使用
- 字符串hash问题
- 黑马程序员——面试题总结
- UVa116
- CloudStack 故障排查汇总-不定期更新
- LintCode : 搜索旋转排序数组
- Java学习——String变量中的双胞胎
- TimeDateStamp中GMT秒数转化为实际时间
- Android常用辅助类(三)——存储相关
- 在POM配置Maven plugin提示错误“Plugin execution not covered by lifecycle configuration”的解决方案