hdu 2825 Wireless Password(AC自动机+状压DP)

题目链接:hdu 2825 Wireless Password

题目大意:N,M,K,M个字符串作为关键码集合,现在要求长度为N,包含K个以上的关键码的字符串有多少个。

解题思路:AC自动机+dp,滚动数组,因为关键码个数不会超过10个,所以我们用二进制数表示匹配的状态。dp[i][j][k]

表示到第i个位置,j节点,匹配k个字符串。

#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <iostream>
#include <algorithm>

using namespace std;

typedef long long ll;

const int maxn = 255;
const int sigma_size = 26;
const int mod = 20090717;

inline int bitcount (int x) {
    return x == 0 ? 0 : bitcount(x>>1) + (x&1);
}

struct Aho_Corasick {
    int sz, g[maxn][sigma_size];
    int tag[maxn], fail[maxn], last[maxn];
    int dp[2][maxn][1030];

    void init();
    int idx(char ch);
    void insert(char* str, int k);
    void getFail();
    void match(char* str);
    int put(int u);
    int solve();
}AC;

int N, M, K;

int main () {
    while (scanf("%d%d%d", &N, &M, &K) == 3 && N + M + K) {
        char w[30];
        AC.init();
        for (int i = 1; i <= M; i++) {
            scanf("%s", w);
            AC.insert(w, i);
        }
        if (K > M)
            printf("0\n");
        else
            printf("%d\n", AC.solve());
    }
    return 0;
}

int Aho_Corasick::solve() {
    getFail();
    memset(dp[0], 0, sizeof(dp[0]));
    dp[0][0][0] = 1;

    for (int x = 0; x < N; x++) {
        int now = x&1;
        int nxt = now^1;
        memset(dp[nxt], 0, sizeof(dp[nxt]));

        for (int i = 0; i < sz; i++) {
            for (int k = 0; k < (1<<M); k++) {
                if (dp[now][i][k] == 0)
                    continue;

                for (int j = 0; j < sigma_size; j++) {
                    int u = i;
                    while (u && g[u][j] == 0)
                        u = fail[u];

                    u = g[u][j];

                    int d = tag[u];
                    dp[nxt][u][k|d] = (dp[nxt][u][k|d] + dp[now][i][k]) % mod;
                }
            }
        }
    }

    int n = 0, v[1030];
    memset(v, 0, sizeof(v));
    for (int i = 0; i < (1<<M); i++)
        if (bitcount(i) >= K)
            v[n++] = i;

    int ans = 0, d = N&1;
    for (int u = 0; u < sz; u++) {
        for (int i = 0; i < n; i++)
            ans = (ans + dp[d][u][v[i]]) % mod;
    }
    return ans;
}

void Aho_Corasick::init() {
    sz = 1;
    tag[0] = 0;
    memset(g[0], 0, sizeof(g[0]));
}

int Aho_Corasick::idx(char ch) {
    return ch - ‘a‘;
}

int Aho_Corasick::put(int u) {
    return 0;
}

void Aho_Corasick::insert(char* str, int k) {
    int u = 0, n = strlen(str);

    for (int i = 0; i < n; i++) {
        int v = idx(str[i]);
        if (g[u][v] == 0) {
            tag[sz] = 0;
            memset(g[sz], 0, sizeof(g[sz]));
            g[u][v] = sz++;
        }
        u = g[u][v];
    }
    tag[u] = (1<<(k-1));
}

void Aho_Corasick::match(char* str) {
    int n = strlen(str), u = 0;
    for (int i = 0; i < n; i++) {
        int v = idx(str[i]);
        while (u && g[u][v] == 0)
            u = fail[u];

        u = g[u][v];
    }
}

void Aho_Corasick::getFail() {
    queue<int> que;

    for (int i  = 0; i < sigma_size; i++) {
        int u = g[0][i];
        if (u) {
            fail[u] = last[u] = 0;
            que.push(u);
        }
    }

    while (!que.empty()) {
        int r = que.front();
        que.pop();

        for (int i = 0; i < sigma_size; i++) {
            int u = g[r][i];

            if (u == 0) {
                g[r][i] = g[fail[r]][i];
                continue;
            }

            que.push(u);
            int v = fail[r];
            while (v && g[v][i] == 0)
                v = fail[v];

            fail[u] = g[v][i];
            tag[u] |= tag[fail[u]];
            //last[u] = tag[fail[u]] ? fail[u] : last[fail[u]];
        }
    }
}
时间: 2024-10-24 11:06:50

hdu 2825 Wireless Password(AC自动机+状压DP)的相关文章

hdu 2825 Wireless Password(ac自动机&amp;dp)

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4022    Accepted Submission(s): 1196 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

HDU 2825 Wireless Password (AC自动机,DP)

http://acm.hdu.edu.cn/showproblem.php?pid=2825 Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4560    Accepted Submission(s): 1381 Problem Description Liyuan lives in a old a

HDU 2825 Wireless Password AC自动机+dp

训练赛第二场的I题,上完体育课回来就把这题过了,今天训练赛rank1了,还把大大队虐了,而且我还过了这道题 (虽然我也就过了这道题...),第一次在比赛中手写AC自动机还带dp的,心情大好. 给一个字符串集合,求包含该集合超过K个字符的,长度为L的字符串的个数. 显然是在AC自动机上跑dp,设dp[u][L][k]表示当前在结点u,还要走L步,当前状态为k的个数.一开始第三维表示的是包含k个字符串,但是题目要求不含重复的,那就只能状压了.转移为dp[u][L][k]+=dp[v][L-1][nk

hdu2825---Wireless Password(AC自动机+状压dp)

Wireless Password Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4688 Accepted Submission(s): 1433 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there was a

hdu_2825_Wireless Password(AC自动机+状压DP)

题目链接:hdu_2825_Wireless Password 题意: 给你m个串,问长度为n至少含k个串的字符串有多少个 题解: 设dp[i][j][k]表示考虑到长度为i,第j个自动机的节点,含有k这个压缩状态的方案数,然后DP下去就行了 1 #include<bits/stdc++.h> 2 #define F(i,a,b) for(int i=a;i<=b;i++) 3 using namespace std; 4 5 const int mod=20090717; 6 cons

hdu 2825 aC自动机+状压dp

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5640    Accepted Submission(s): 1785 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

hdu 2825 Wireless Password(ac自己主动机&amp;amp;dp)

Wireless Password Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4022    Accepted Submission(s): 1196 Problem Description Liyuan lives in a old apartment. One day, he suddenly found that there

HDU 3341 Lost&#39;s revenge(AC自动机+状压DP)

Lost's revenge Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 4548    Accepted Submission(s): 1274 Problem Description Lost and AekdyCoin are friends. They always play "number game"(A bor

poj 1699 Best Sequence(AC自动机+状压DP)

题目链接:poj 1699 Best Sequence 题目大意:给定N个DNA序列,问说最少多长的字符串包含所有序列. 解题思路:AC自动机+状压DP,先对字符串构造AC自动机,然后在dp[s][i]表示匹配了s,移动到节点i时候的最短步数. #include <cstdio> #include <cstring> #include <queue> #include <vector> #include <iostream> #include &