Remember the Word 【UVALive - 394】【AC自动机 + DP】

版权声明:https://blog.csdn.net/qq_41730082 https://blog.csdn.net/qq_41730082/article/details/86590014

题目链接


  很多人用了Trie图去做了这道题,或许那个做法确实会快得多,但是,我却想到了在AC自动机上写了个DP,与2018四川省赛(第十届四川省ACM)那道题一样,甚至还要略微简单一些,这道题就敲出来了,它不用记录路径,直接输出最后的解即可,嗯……好题。嘤嘤嘤……


#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 3e5 + 7;
const int mod = 20071027;
int N, dp[maxN], lten;
char test[maxN], virus[105];
struct node
{
    node *next[26];
    node *fail, *las;
    int sum, len;
    node()
    {
        for(int i=0; i<26; i++) next[i] = NULL;
        fail = NULL;    las = NULL;
        sum = len = 0;
    }
};
node *root;
void update(char *s)
{
    node *temp = root;
    int len = (int)strlen(s);
    for(int i=0; i<len; i++)
    {
        int x = s[i] - 'a';
        if(temp->next[x] == NULL) temp->next[x] = new node();
        temp = temp->next[x];
    }
    temp->sum = 1;
    temp->len = len;
}
void build_fail()
{
    queue<node *> Q;
    Q.push(root);
    node *temp, *p;
    while(!Q.empty())
    {
        temp = Q.front();   Q.pop();
        for(int i=0; i<26; i++)
        {
            if(temp->next[i])
            {
                if(temp == root) { temp->next[i]->fail = root; temp->next[i]->las = root; }
                else
                {
                    p = temp->fail;
                    while(p)
                    {
                        if(p->next[i]) { temp->next[i]->fail = p->next[i];  break; }
                        p = p->fail;
                    }
                    if(!p) { temp->next[i]->fail = root; temp->next[i]->las = root; }
                    else
                    {
                        if(p->next[i]->sum) temp->next[i]->las = p->next[i];
                        else temp->next[i]->las = p->next[i]->las;
                    }
                }
                Q.push(temp->next[i]);
            }
        }
    }
}
void AC_auto()  //对test的处理啦
{
    node *p = root;
    for(int i=1; i<=lten; i++)
    {
        int x = test[i] - 'a';
        while(p!=root && !p->next[x]) p = p->fail;
        p = p->next[x];
        if(!p) p = root;
        node *temp = p;
        while(temp && temp != root)
        {
            if(temp->sum)
            {
                dp[i] += dp[i - temp->len];
                if(dp[i] >= mod) dp[i] -= mod;
            }
            temp = temp->las;
        }
    }
}
inline void init()
{
    lten = (int)strlen(test + 1);
    dp[0] = 1;
    for(int i=1; i<=lten; i++) dp[i] = 0;
    root = (node *)malloc(sizeof(node));
    for(int i=0; i<26; i++) root->next[i] = NULL;
    root->fail = NULL;  root->las = NULL;
    root->sum = 0;
}
int main()
{
    int Cas = 0;
    while(scanf("%s", test + 1)!=EOF)
    {
        init();
        scanf("%d", &N);
        for(int i=1; i<=N; i++)
        {
            scanf("%s", virus);
            update(virus);
        }
        build_fail();
        AC_auto();
        printf("Case %d: %d\n", ++Cas, dp[lten]);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_41730082/article/details/86590014
今日推荐