题目链接:见这里
题意:
N≤2×106的母串,M≤500的模式串
模式串的每个字符ci有cnti≤62个可选字符
求母串哪些位置可以匹配模式串

解题思路:
dp[i][j]:=母串匹配到i,模式串匹配到j,能否匹配
把所有可选字符压起来成LL,复杂度是O(nm)的,并不行
dp[i][j]=dp[i−1][j−1],t[j]>>s[i] and 1为真
由于dp状态是bool,并且状态只与i−1有关,考虑bitset压位
预处理出模式串字符集在模式串的位置到bitset中
每次转移只要左移一次然后与母串字符的状态即可
这样就能一次直接转移整个模式串的状态
时间复杂度为O(nm/64)。这个和上一篇博客简直是太一样了哈。

代码如下:

#include <bits/stdc++.h>
using namespace std;
const int N = 2e6 + 10;
bitset <510> dp[2], b[256];
char s[N], t[510];
int n;
int main()
{
    while(gets(s+1))
    {
        for(int i = 0; i < 256; i++) b[i].reset();
        scanf(" %d", &n);
        for(int i = 1; i <= n; i++){
            int x;
            scanf("%d%s", &x, t + 1);
            for(int j = 1; j <= x; j++){
                b[t[j]][i] = 1;
            }
        }
        bool ok = false;
        int p = 0;
        dp[p].reset();
        dp[p][0] = 1;
        for(int i = 1; s[i]; i++){
            dp[!p] = (dp[p] << 1) & b[s[i]];
            dp[!p][0] = 1;
            if(dp[!p][n]){
                ok = true;
                printf("%d\n", i - n + 1);
            }
            p = !p;
        }
        if(!ok) puts("NULL");
        scanf("%*c");
    }
    return 0;
}