Wireless Password :ac自动机 + 状压DP

分析

f[i][j][k]表示第i位的情况下,在trie图上的位置位j的情况下匹配情况位k,状态转移方程就比较好写了

dp[i + 1][tr[j][c]][l | cnt[tr[j][c]]] = (dp[i + 1][tr[j][c]][l | cnt[tr[j][c]]] + dp[i][j][l]) % mod

注意一下减去不合法的状态,节约时间

代码


#pragma GCC optimize(3)
#include <bits/stdc++.h>
#define debug(x) cout<<#x<<":"<<x<<endl;
#define dl(x) printf("%lld\n",x);
#define di(x) printf("%d\n",x);
#define _CRT_SECURE_NO_WARNINGS
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> PII;
typedef vector<int> VI;
const int INF = 0x3f3f3f3f;
const int N = 1e3 + 10;
const int mod= 20090717;
const double eps = 1e-9;
const double PI = acos(-1);
template<typename T>inline void read(T &a){
    
    char c=getchar();T x=0,f=1;while(!isdigit(c)){
    
    if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){
    
    x=(x<<1)+(x<<3)+c-'0';c=getchar();}a=f*x;}
int gcd(int a,int b){
    
    return (b>0)?gcd(b,a%b):a;}
int tr[N][26],cnt[N],idx;
int ne[N];
int q[N];
char str[N];
int dp[30][110][1<<10];
int n,m,k;

void init(){
    
    
    memset(tr,0,sizeof tr);
    memset(ne,0,sizeof ne);
    memset(cnt,0,sizeof cnt);
    memset(dp,0,sizeof dp);
    idx = 0;
}

int bits(int x){
    
    
    int res = 0;
    while(x){
    
    
        if(x & 1) res++;
        x >>= 1;
    }
    return res;
}

void insert(int x){
    
    
    int p = 0;
    for(int i = 0;str[i];i++){
    
    
        int t = str[i] - 'a';
        if(!tr[p][t]) tr[p][t] = ++idx;
        p = tr[p][t];
    }
    cnt[p] = (1 << x);
}

void build(){
    
    
    int hh = 0,tt = -1;
    for(int i = 0;i < 26;i++)
        if(tr[0][i])
            q[++tt] = tr[0][i]; 
    while(hh <= tt){
    
    
        int t = q[hh++];
        for(int i = 0;i < 26;i++){
    
    
            int c = tr[t][i];
            if(!c) tr[t][i] = tr[ne[t]][i];
            else{
    
    
                ne[c] = tr[ne[t]][i];
                q[++tt] = c;
                cnt[c] |= cnt[ne[c]];
            }
        }
    }
}

int main(){
    
    
    while(scanf("%d%d%d",&n,&m,&k)){
    
    
        if(n == 0 && m == 0 && k == 0) break;
        init();
        dp[0][0][0] = 1;
        for(int i = 0;i < m;i++){
    
    
            scanf("%s",str);
            insert(i);
        }
        build();
        for(int i = 0;i < n;i++)
            for(int j = 0;j <= idx;j++)
                for(int l = 0;l < (1 << m);l++){
    
    
                    if(!dp[i][j][l]) continue;
                    for(int c = 0;c < 26;c++)
                        dp[i + 1][tr[j][c]][l | cnt[tr[j][c]]] = (dp[i + 1][tr[j][c]][l | cnt[tr[j][c]]] + dp[i][j][l]) % mod;
                }
        int ans = 0;
        for(int i = 0;i < (1 << m);i++){
    
    
            if(bits(i) < k) continue;
            for(int j = 0;j <= idx;j++)
                ans = (ans + dp[n][j][i]) % mod;
        }
        di(ans);
    }
    return 0;
}

/**
*  ┏┓   ┏┓+ +
* ┏┛┻━━━┛┻┓ + +
* ┃       ┃
* ┃   ━   ┃ ++ + + +
*  ████━████+
*  ◥██◤ ◥██◤ +
* ┃   ┻   ┃
* ┃       ┃ + +
* ┗━┓   ┏━┛
*   ┃   ┃ + + + +Code is far away from  
*   ┃   ┃ + bug with the animal protecting
*   ┃    ┗━━━┓ 神兽保佑,代码无bug 
*   ┃        ┣┓
*    ┃        ┏┛
*     ┗┓┓┏━┳┓┏┛ + + + +
*    ┃┫┫ ┃┫┫
*    ┗┻┛ ┗┻┛+ + + +
*/

猜你喜欢

转载自blog.csdn.net/tlyzxc/article/details/113792356
今日推荐