poj 1625 Censored!(AC自动机+DP+高精度)

题目链接:poj 1625 Censored!

题目大意:给定N,M,K,然后给定一个N字符的字符集和,现在要用这些字符组成一个长度为M的字符串,要求不包

括K个子字符串。

解题思路:AC自动机+DP+高精度。这题恶心的要死,给定的不能匹配字符串里面有负数的字符情况,也算是涨姿势

了,对应每个字符固定偏移128单位。

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

using namespace std;

typedef long long ll;

const int maxn = 505;
const int sigma_size = 256;
const int bw = 128;

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

    void init();
    int idx(char ch);
    void insert(char* str, int k);
    void getFail();
    void match(char* str);
    void put(int x, int y);
}A;

struct bign {
    int len, num[100];

    bign () {
        len = 0;
        memset(num, 0, sizeof(num));
    }
    bign (int number) {*this = number;}
    bign (const char* number) {*this = number;}

    void delzero ();
    void Put ();

    void operator = (int number);
    void operator = (char* number);

    bool operator <  (const bign& b) const;
    bool operator >  (const bign& b) const { return b < *this; }
    bool operator <= (const bign& b) const { return !(b < *this); }
    bool operator >= (const bign& b) const { return !(*this < b); }
    bool operator != (const bign& b) const { return b < *this || *this < b;}
    bool operator == (const bign& b) const { return !(b != *this); }

    void operator ++ ();
    void operator -- ();
    bign operator + (const int& b);
    bign operator + (const bign& b);
    bign operator - (const int& b);
    bign operator - (const bign& b);
    bign operator * (const int& b);
    bign operator * (const bign& b);
    bign operator / (const int& b);
    int operator % (const int& b);
}dp[2][maxn];

int N, M, K, tab[55];
char s[55];

void solve() {
    int n = A.sz;
    for (int i = 0; i < n; i++)
        dp[0][i] = 0;
    dp[0][0] = 1;

    for (int i = 0; i < M; i++) {
        int now = i&1, nxt = !(i&1);
        for (int i = 0; i < n; i++) dp[nxt][i] = 0;

        for (int i = 0; i < n; i++) {
            if (A.tag[i] || A.last[i])
                continue;

            for (int j = 0; j < N; j++) {
                int v = tab[j], u = i;
                while (u && A.g[u][v] == 0)
                    u = A.fail[u];
                u = A.g[u][v];
                dp[nxt][u] = dp[nxt][u] + dp[now][i];
            }
        }
    }

    int now = (M&1);
    bign ans = 0;
    for (int i = 0; i < n; i++) {
        if (A.tag[i] || A.last[i])
            continue;
        ans = ans + dp[now][i];
    }
    ans.Put();
    printf("\n");
}

int main () {
    while (scanf("%d%d%d%*c", &N, &M, &K) == 3) {
        A.init();
        gets(s);
        for (int i = 0; i < N; i++)
            tab[i] = s[i] + bw;

        for (int i = 1; i <= K; i++) {
            gets(s);
            A.insert(s, i);
        }
        A.getFail();
        solve();
    }
    return 0;
}

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

int Aho_Corasick::idx(char ch) {
    return ch + bw;
}

void Aho_Corasick::put(int x, int y) {
}

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] = k;
}

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];

        if (tag[u])
            put(i, u);
        else if (last[u])
            put(i, last[u]);
    }
}

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];
            last[u] = tag[fail[u]] ? fail[u] : last[fail[u]];
        }
    }
}

void bign::delzero () {
    while (len && num[len-1] == 0)
        len--;

    if (len == 0) {
        num[len++] = 0;
    }
}

void bign::Put () {
    for (int i = len-1; i >= 0; i--)
        printf("%d", num[i]);
}

void bign::operator = (char* number) {
    len = strlen (number);
    for (int i = 0; i < len; i++)
        num[i] = number[len-i-1] - ‘0‘;

    delzero ();
}

void bign::operator = (int number) {

    len = 0;
    while (number) {
        num[len++] = number%10;
        number /= 10;
    }

    delzero ();
}

bign bign::operator + (const int& b) {
    bign a = b;
    return *this + a;
}

bign bign::operator + (const bign& b) {
    int bignSum = 0;
    bign ans;

    for (int i = 0; i < len || i < b.len; i++) {
        if (i < len) bignSum += num[i];
        if (i < b.len) bignSum += b.num[i];

        ans.num[ans.len++] = bignSum % 10;
        bignSum /= 10;
    }

    while (bignSum) {
        ans.num[ans.len++] = bignSum % 10;
        bignSum /= 10;
    }

    return ans;
}
时间: 2024-10-25 13:08:12

poj 1625 Censored!(AC自动机+DP+高精度)的相关文章

POJ 1625 Censored!(AC自动机,DP)

http://poj.org/problem?id=1625 Censored! Time Limit: 5000MS   Memory Limit: 10000K Total Submissions: 8266   Accepted: 2229 Description The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish)

POJ 1625 Censored ( Trie图 &amp;&amp; DP &amp;&amp; 高精度 )

题意 : 给出 n 个单词组成的字符集 以及 p 个非法串,问你用字符集里面的单词构造长度为 m 的单词的方案数有多少种? 分析 :先构造出 Trie 图方便进行状态转移,这与在 POJ 2278 中的步骤是一样的,只不过最后的DP状态转移方式 2778 是利用了矩阵进行转移的,那是因为需要构造的串的长度非常长!只能利用矩阵转移.但是这道题需要构造的串的长度最多也就只有 50 ,可以利用普通的DP方法进行转移.我们定义 DP[i][j] 为以长度为 i 以字符 j 为结尾的串的种类数是多少,那么

POJ1625---Censored!(AC自动机+dp+高精度)

Description The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters without word breaks. So, there exist exactly N^M different Freish sentences. But after recent

[AC自动机+dp+高精度] poj 1625 Censored!

题意: 给一个长度为N的字符串,表示有N个字符可用. 再给p个不能含有的病毒串. 为你长度为M的串不含有任意一个病毒串的方案数. 思路: 由于不需要取模,50^50需要用到高精度. 因为题目的字符串大于32所以可以直接scanf输入. 输入完map一下就好了. 就是裸的自动机dp了. 代码: #include"stdio.h" #include"algorithm" #include"string.h" #include"iostrea

poj 1625 (AC自动机好模版,大数好模版)

题目 给n个字母,构成长度为m的串,总共有n^m种.给p个字符串,问n^m种字符串中不包含(不是子串)这p个字符串的个数. 将p个不能包含的字符串建立AC自动机,每个结点用val值来标记以当前节点为后缀的字符串是否包含非法字符串(p个字符串中的任何一个). 状态转移方程:f(i, j)  += f(i-1, k) f(i, j)表示长度为i的字符串,结尾为字符j,方程j和k的关系可以从自动机中失配关系直接获得(j是k的后继结点). 总之感觉是好东西,快存下来 大数模版: #include <cs

POJ1625 Censored!(AC自动机+DP)

题目问长度m不包含一些不文明单词的字符串有多少个. 依然是水水的AC自动机+DP..做完后发现居然和POJ2778是一道题,回过头来看都水水的... dp[i][j]表示长度i(在自动机转移i步)且后缀状态为自动机第j个结点的合法字符串数 dp[0][0]=1 转移转移... 注意要用高精度,因为答案最多5050. 还有就是要用unsigned char,题目的输入居然有拓展的ASCII码,编码128-255. 1 #include<cstdio> 2 #include<cstring&

poj 1625 Censored!

Censored! http://poj.org/problem?id=1625 Time Limit: 5000MS   Memory Limit: 10000K       Description The alphabet of Freeland consists of exactly N letters. Each sentence of Freeland language (also known as Freish) consists of exactly M letters witho

HDU 2243 考研路茫茫――单词情结 (AC自动机 + dp)

HDU 2243 考研路茫茫――单词情结 题意:给定一些词根,如果一个单词包含有词根,则认为是有效的.现在问长度不超过L的单词里面,有多少有效的单词? 思路:这道题和POJ 2778是同样的思路.POJ 2778是要找出长度为L的单词里面有多少无效的单词.那么根据同样的方法构造矩阵,然后所有无效的单词个数为 A + A^2 + ... + A^l 个.而所有单词的个数为26 + 26^2 + - + 26^l 个.两个减一下即为答案. 矩阵连乘求和:I + A^2 + A^3 + ... + A

HDU3341 Lost&#39;s revenge(AC自动机+DP)

题目是给一个DNA重新排列使其包含最多的数论基因. 考虑到内存大概就只能这么表示状态: dp[i][A][C][G][T],表示包含各碱基个数为ACGT且当前后缀状态为自动机第i的结点的字符串最多的数论基因数 其中ACGT可以hash成一个整数(a*C*G*T+c*G*T+g*T+T),这样用二维数组就行了,而第二维最多也就11*11*11*11个. 接下来转移依然是我为人人型,我是丢进一个队列,用队列来更新状态的值. 这题果然挺卡常数的,只好手写队列,最后4500msAC,还是差点超时,代码也