洛谷P4591 [TJOI2018]碱基序列(hash dp)
程序员文章站
2022-12-14 23:29:35
题意 "题目链接" Sol $f[i][j]$表示匹配到第$i$个串,当前在主串的第$j$个位置 转移的时候判断一下是否可行就行了。随便一个能搞字符串匹配的算法都能过 复杂度$O(|S| K a_i)$ cpp include define Pair pair define MP(x, y) mak ......
题意
sol
\(f[i][j]\)表示匹配到第\(i\)个串,当前在主串的第\(j\)个位置
转移的时候判断一下是否可行就行了。随便一个能搞字符串匹配的算法都能过
复杂度\(o(|s| k a_i)\)
#include<bits/stdc++.h> #define pair pair<int, int> #define mp(x, y) make_pair(x, y) #define fi first #define se second //#define int long long #define ull signed long long #define ll long long #define fin(x) {freopen(#x".in","r",stdin);} #define fout(x) {freopen(#x".out","w",stdout);} using namespace std; const int maxn = 3e6 + 10, mod = 1e9 + 7, inf = 1e9 + 10; const double eps = 1e-9; template <typename a, typename b> inline bool chmin(a &a, b b){if(a > b) {a = b; return 1;} return 0;} template <typename a, typename b> inline bool chmax(a &a, b b){if(a < b) {a = b; return 1;} return 0;} template <typename a, typename b> inline ll add(a x, b y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;} template <typename a, typename b> inline void add2(a &x, b y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);} template <typename a, typename b> inline ll mul(a x, b y) {return 1ll * x * y % mod;} template <typename a, typename b> inline void mul2(a &x, b y) {x = (1ll * x * y % mod + mod) % mod;} template <typename a> inline void debug(a a){cout << a << '\n';} template <typename a> inline ll sqr(a x){return 1ll * x * x;} inline int read() { char c = getchar(); int x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();} while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar(); return x * f; } int k; char s[maxn], tmp[maxn]; int f[101][100001]; ull ha[maxn], po[maxn], base = 27; ull query(int l,int r) { return ha[r] - po[r - l + 1] * ha[l - 1]; } signed main() { k = read(); scanf("%s", s + 1); int n = strlen(s + 1); po[0] = 1; for(int i = 1; i <= n; i++) po[i] = base * po[i - 1], ha[i] = ha[i - 1] * base + s[i]; for(int i = 0; i <= n; i++) f[0][i] = 1; for(int i = 1; i <= k; i++) { int num = read(); for(int j = 1; j <= num; j++) { scanf("%s", tmp + 1); int l = strlen(tmp + 1); ull val = 0; for(int k = 1; k <= l; k++) val = val * base + tmp[k]; for(int k = l; k <= n; k++) { if(val == query(k - l + 1, k)) { add2(f[i][k], f[i - 1][k - l]); } } } } int ans = 0; for(int i = 1; i <= n; i++) add2(ans, f[k][i]); cout << ans; return 0; } /* */
上一篇: 总用人家倒车镜被发现了多尴尬啊