首页 技术 正文
技术 2022年11月8日
0 收藏 457 点赞 1,665 浏览 2062 个字

bzoj4641 基因改造 KMP / hash

依稀记得,$NOIP$之前的我是如此的弱小….

完全不会$KMP$的写法,只会暴力$hash$….

大体思路为把一个串的哈希值拆成$26$个字母的位权

即$hash(S) = \sum\limits_{a} a * \sum w^i * [s[i] == a]$

通过记录每个字母第一次出现的位置,用$26$的时间来确定$f$是什么

然后通过确定的$f$计算出$f$是正确的时候的$hash$值,和原串的$hash$值比较

复杂度$O(26n)$

自然取模….

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
namespace remoon {
#define ri register int
#define ll long long
#define ull unsigned long long
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --)
}
using namespace std;
using namespace remoon;#define sid 500050
char s[], t[sid];int n, m, tim, f[];
int num[], vis[], nxt[], tot;
ull val[], wei[sid];
ull seed = ;inline void Init() {
wei[] = ;
rep(i, , m) wei[i] = wei[i - ] * seed;
rep(i, , m) {
int le = t[i];
if(!vis[le]) nxt[++ tot] = i, vis[le] = ;
val[le] += wei[m - i];
}
tim ++;
}inline void Solve() {
ull now = , tval = ;
rep(i, , m) now += s[i] * wei[m - i];
rep(i, m, n) { tim ++;
int flag = ; rep(j, , tot) {
int v = s[i - m + nxt[j]];
if(vis[v] == tim) { flag = ; break; }
if(vis[v] != tim) vis[v] = tim;
f[j] = v;
} if(!flag) {
tval = ;
rep(j, , tot)
tval += f[j] * val[t[nxt[j]]];
if(tval == now) write(i - m + );
} now -= s[i - m + ] * wei[m - ];
now *= seed; now += s[i + ];
}
}int main() {
scanf("%s", s + ); n = strlen(s + );
scanf("%s", t + ); m = strlen(t + );
Init(); Solve();
return ;
}

现在我明白了$KMP$是非常伟大的算法….

对于此题而言,考虑每个字符的上一个字符离当前字符的距离,这可以成为一个新串

然后比对新串即可

特别的,如果上一个字符出现的位置超过了匹配长度,那么我们也要视作合法

但是,我们发现这种匹配满足有前效性,没有后效性,因此可以用$KMP$

复杂度$O(n)$,十分的优秀

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
namespace remoon {
#define ri register int
#define rep(io, st, ed) for(ri io = st; io <= ed; io ++)
#define drep(io, ed, st) for(ri io = ed; io >= st; io --)
}
using namespace std;
using namespace remoon;const int sid = ;int n, m;
char s[sid], t[sid];
int lst[], S[sid], T[sid], nxt[sid];inline bool match(int x, int y) {
if(y > m) return ;
if(x == T[y]) return ;
if(!T[y] && x >= y) return ;
return ;
}int main() {
scanf("%s", s + ); scanf("%s", t + );
n = strlen(s + ); m = strlen(t + ); rep(i, , n) {
S[i] = lst[s[i]] ? i - lst[s[i]] : ;
lst[s[i]] = i;
} memset(lst, , sizeof(lst));
rep(i, , m) {
T[i] = lst[t[i]] ? i - lst[t[i]] : ;
lst[t[i]] = i;
} for(ri i = , j = ; i <= m; i ++) {
while(j && !match(T[i], j + )) j = nxt[j];
if(match(T[i], j + )) j ++;
nxt[i] = j;
} for(ri i = , j = ; i <= n; i ++) {
while(j && !match(S[i], j + )) j = nxt[j];
if(match(S[i], j + )) j ++;
if(j == m) printf("%d\n", i - m + );
}
return ;
}
上一篇: 试水jdk8 stream
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,489
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,904
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,737
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,490
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,128
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,291