poj3461 hash字符串匹配
来源:互联网 发布:机顶盒点播软件 编辑:程序博客网 时间:2024/06/05 15:38
可以用KMP写,然而发现hash不用写很长,而且好理解。
定义一个匹配串的hash值为h[m] = c[1]b^ (m - 1) + c[2]b^(m - 2) + …… + c[m - 1]b + c[m],然后向后推的时候h[i] = h[i - 1] - c[i - m] * pow(b,m) + c[i];
然后直接比较在模式串上子串的hash值是不是等于匹配串,是的话就匹配了。
b的选择一般是质数,为了溢出也是正数一般数都定义为usigned long long;
//#include<bits/stdc++.h>#include<iostream>#include<cstdio>#include<cstring>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef pair<int,int> P;#define fi first#define se second#define INF 0x3f3f3f3f#define clr(x,y) memset(x,y,sizeof x)#define PI acos(-1.0)#define ITER set<int>::iteratorconst int Mod = 1e9 + 7;const int maxn = 1000000 + 10;const int N = 2;const ull M = 133;char s[maxn],t[maxn];ull p[maxn];int main(){ int Tcase;scanf("%d",&Tcase); while(Tcase --) { scanf("%s%s",s,t);int len1 = strlen(s),len2 = strlen(t); ull ah = 0, bh = 0;ull temp = 1;for(int i = 0; i < len1; i ++)temp *= M; for(int i = 0;i < len1; i ++)ah = ah * M + s[i],bh = bh * M + t[i]; int ans = 0; for(int i = len1 - 1; i < len2; i ++) { if(bh == ah)ans ++;bh = bh * M - temp * t[i - len1 + 1] + t[i + 1]; } printf("%d\n",ans); } return 0;}
阅读全文
0 0
- poj3461 hash字符串匹配
- POJ3461 字符串HASH
- POJ3461 字符串hash
- POJ3461 字符串双HASH
- POJ3461 字符串双HASH
- POJ3461 KMP快速字符串匹配
- POJ3461 Oulipo(字符串匹配KMP)
- POJ3461-字符串匹配(基础)
- POJ3461【1e6 字符串 hash模板】
- poj3461 KMP算法(字符串匹配)
- Hash 匹配字符串模板
- POJ3461 字符串哈希
- POJ3461 字符串之模式匹配,kmp,求目标串中有多少个位置可重叠的子串
- poj 3461 字符串单串匹配--KMP或者字符串HASH
- POJ3461-串匹配-经典的KMP
- poj3461
- poj3461
- poj3461
- grails应用和mybatis应用关于oracle的整合
- JAVA获取CLASSPATH路径
- 「python」继承
- Winchill 获取发布结构子对象WTPart
- myeclipse远程调试tomcat7
- poj3461 hash字符串匹配
- Electron 实战桌面计算器应用
- Spring Cloud Config (1)
- apache httpclien访问乱码,返回信息乱码问题
- xamarin android实现Toolbar+DrawerLayout完美侧滑导航栏
- 我看Python代码缩进
- CentOS6.2修改网卡名称
- vue饿了么学习-第五篇(项目结构)
- 九度 题目1056:最大公约数