字符串hash问题
2015-10-11 15:33
316 查看
一些介绍字符串hash的文章:
字符串哈希函数
13年长春现场赛的字符串水题。。
不过没有接触过这类题,所以不会做。。
所谓字符串hash,通常是把一个字符串映射为一个整数。
我这里采用的是BKDRHash函数。稳定性最好的一个字符串hash函数。
AC代码:
字符串哈希函数
String
题目传送:HDU - 4821 - String13年长春现场赛的字符串水题。。
不过没有接触过这类题,所以不会做。。
所谓字符串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 0x7fffffff using namespace std; #define ULL unsigned long long const 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; }
相关文章推荐
- Android之获取手机上的图片和视频缩略图thumbnails
- android string.xml文件中的整型和string型代替
- Android java 与 javascript互访(相互调用)的方法例子
- c语言实现hashmap(转载)
- android上改变listView的选中颜色
- String.intern
- Mootools 1.2教程 函数
- autoit InputBox 函数
- 文件遍历排序函数
- Prototype源码浅析 String部分(二)
- Ruby中的String对象学习笔记
- Oracle 函数大全[字符串函数,数学函数,日期函数]第1/4页
- ASP下经常用的字符串等函数参考资料
- PostgreSQL教程(五):函数和操作符详解(1)
- DOS批处理 函数定义与用法
- asp Chr 函数 数字转字母的方法
- Lua中的函数精讲笔记
- Lua中的闭合函数、非全局函数与函数的尾调用详解
- Lua中调用C++函数示例
- Lua实现split函数