hdu2604---Queuing(AC自动机+矩阵)

Problem Description
Queues and Priority Queues are data structures which are known to most computer scientists. The Queue occurs often in our daily life. There are many people lined up at the lunch time.

Now we define that ‘f’ is short for female and ‘m’ is short for male. If the queue’s length is L, then there are 2L numbers of queues. For example, if L = 2, then they are ff, mm, fm, mf . If there exists a subqueue as fmf or fff, we call it O-queue else it is a E-queue.
Your task is to calculate the number of E-queues mod M with length L by writing a program.

Input
Input a length L (0 <= L <= 10 6) and M.

Output
Output K mod M(1 <= M <= 30) where K is the number of E-queues with length L.

Sample Input

3 8 4 7 4 8

Sample Output

6 2 1

Author
WhereIsHeroFrom

Source
HDU 1st “Vegetable-Birds Cup” Programming Open Contest

Recommend
lcy | We have carefully selected several similar problems for you: 1588 1757 2606 2276 2603

设dp[i][j]表示长度为i,在节点j时,不含非法串的方案数
由于n很大,直接递推会超时,所以用矩阵来加速
如果节点i可以转移到节点j,那么A.mat[i][j] = 1
最后快速幂求出A^n次,然后统计答案即可

/************************************************************************* > File Name: hdu2604.cpp > Author: ALex > Mail: [email protected] > Created Time: 2015年03月10日 星期二 20时30分24秒 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const double pi = acos(-1.0);
const int inf = 0x3f3f3f3f;
const double eps = 1e-15;
typedef long long LL;
typedef pair <int, int> PLL;

const int MAX_NODE = 20;
const int CHILD_NUM = 2;

int m;

struct MARTIX
{
    int mat[MAX_NODE][MAX_NODE];
};

MARTIX mul (MARTIX a, MARTIX b, int L)
{
    MARTIX c;
    for (int i = 0; i < L; ++i)
    {
        for (int j = 0; j < L; ++j)
        {
            c.mat[i][j] = 0;
            for (int k = 0; k < L; ++k)
            {
                c.mat[i][j] += a.mat[i][k] * b.mat[k][j];
                c.mat[i][j] %= m;
            }
        }
    }
    return c;
}

MARTIX fastpow (MARTIX ret, int n, int L)
{
    MARTIX ans;
    for (int i = 0; i < L; ++i)
    {
        for (int j = 0; j < L; ++j)
        {
            ans.mat[i][j] = (i == j);
        }
    }
    while (n)
    {
        if (n & 1)
        {
            ans = mul (ans, ret, L);
        }
        n >>= 1;
        ret = mul (ret, ret, L);
    }
    return ans;
}

struct AC_Automation
{
    int next[MAX_NODE][CHILD_NUM];
    int fail[MAX_NODE];
    int end[MAX_NODE];
    int root, L;

    int newnode()
    {
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            next[L][i] = -1;
        }
        end[L++] = 0;
        return L - 1;
    }

    void init()
       {
             L = 0;
            root = newnode();
        }

    void Build_Trie(char buf[])
    {
        int now = root;
        int len = strlen(buf);
        for (int i = 0; i < len; ++i)
        {
            if (next[now][buf[i] - '0'] == -1)
            {
            next[now][buf[i] - '0'] = newnode();
            }
            now = next[now][buf[i] - '0'];
        }
        end[now] = 1;
        }

    void Build_AC()
    {
        queue <int> qu;
        fail[root] = root;
        for (int i = 0; i < CHILD_NUM; ++i)
        {
            if (next[root][i] == -1)
            {
                next[root][i] = root;
            }
            else
            {
                fail[next[root][i]] = root;
                qu.push(next[root][i]);
            }
        }
        while (!qu.empty())
        {
            int now = qu.front();
            qu.pop();
            if (end[fail[now]])
            {
                end[now] = 1;
            }
            for (int i = 0; i < CHILD_NUM; ++i)
            {
                if (next[now][i] == -1)
                {
                    next[now][i] = next[fail[now]][i];
                }
                else
                {
                    fail[next[now][i]] = next[fail[now]][i];
                    qu.push(next[now][i]);
                }
            }
        }
    }

    void solve (int n)
    {
        MARTIX ret;
        for (int i = 0; i < L; ++i)
        {
            for (int j = 0; j < L; ++j)
            {
                ret.mat[i][j] = 0;
            }
        }
        for (int i = 0; i < L; ++i)
        {
            if (end[i])
            {
                continue;
            }
            for (int j = 0; j < CHILD_NUM; ++j)
            {
                if (end[next[i][j]])
                {
                    continue;
                }
                ++ret.mat[i][next[i][j]];
            }
        }
        MARTIX c = fastpow (ret, n, L);
        int ans = 0;
        for (int i = 0; i < L; ++i)
        {
            ans += c.mat[0][i];
            ans %= m;
        }
        printf("%d\n", ans);
    }
}AC;


int main ()
{
    int L;
    char buf1[] = "010";
    char buf2[] = "000";
    while (~scanf("%d%d", &L, &m))
    {
        AC.init();
        AC.Build_Trie (buf1);
        AC.Build_Trie (buf2);
        AC.Build_AC();
        AC.solve (L);
    }
    return 0;
}

你可能感兴趣的:(矩阵,AC自动机)