AC自动机专题——H - Text Generator SPOJ - GEN 矩阵快速幂+AC自动机

LoadingTime has been given a task these days. He is required to write a tool called Text Generator. This software is widely used among the kids who are under seven. It generates an article with the size of a given number L for users. If an article contains at least one word which the users know, we consider it readable. Now, LoadingTime wants to know, how many readable articles can it generates, and he can improve his Text Generator. Could you help him??

Input

The input contains multiple test cases.

The first line of each test case contains two integer N (1 <= N <= 10), L (1 <= L <= 1000000). The following N lines contain N words representing the words knew by the users. All the words and the generated article only contain uppercase letters, and the length of each word is not greater than 6.

Output

For each test case, your program should output a integer as LoadingTime required. As the number could be quite large, you only need to print the answer modulo 10007.

Example

Input:
2 2
A
B
2 10000
ABC
B

Output:
100
5960



题目大意:与G题相反,其要求的是有多少包含某些字符串长度为N的字符串。

这里利用容斥原理,总的减去不包含的,剩下的就是包含的,想到这里,此题与上一题就没有什么区别了。


注:此题时间限制很严格,建议多优化


Status Accepted
Time 740ms
Memory 2867kB
Length 5100
Lang C++ (g++ 4.3.2)
Submitted
Shared
RemoteRunId 19205487
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <iomanip>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(int i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
const int inf_int = 2e9;
const long long inf_ll = 2e18;
#define inf_add 0x3f3f3f3f
#define MOD 10007
#define pb push_back
//#define mp make_pair
#define fi first
#define se second
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=5e2+10;
using namespace std;
typedef  vector<int> vi;
typedef  long long ll;
typedef  unsigned long long  ull;
typedef  unsigned int  ul;
inline int read(){int ra,fh;char rx;rx=getchar(),ra=0,fh=1;
while((rx<'0'||rx>'9')&&rx!='-')rx=getchar();if(rx=='-')
fh=-1,rx=getchar();while(rx>='0'&&rx<='9')ra*=10,ra+=rx-48,
rx=getchar();return ra*fh;}
//#pragma comment(linker, "/STACK:102400000,102400000")
ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}

ll qpow(ll p,ll q){
    ll f=1;
    while(q)
    {
        if(q&1) f=f*p%MOD;
        p=p*p%MOD;
        q>>=1;
    }
    return f;
}

struct Matrix
{
    int len;
    ul mat[70][70];
    Matrix() {}
    Matrix(int L)
    {
        len = L;
    }
};

Matrix mult(Matrix ta,Matrix tb)
{
    Matrix tc;
    tc.len = ta.len;
    for(int i = 0; i < ta.len; i++)
    {
        for(int j = 0; j < ta.len; j++)
        {
            tc.mat[i][j] = 0;
            for(int k = 0; k < ta.len; k++){
                tc.mat[i][j] = tc.mat[i][j]+ta.mat[i][k]*tb.mat[k][j];
            }
            tc.mat[i][j] %= MOD;
        }
    }
    return tc;
}

Matrix pow_mat(Matrix a,int n)
{
    Matrix cnt = a;
    n--;
    while(n)
    {
        if(n&1) cnt = mult(cnt,a);
        a = mult(a,a);
        n >>= 1;
    }
    return cnt;
}



const int N = 70;
const int M = 26;

class Trie
{

public:
    int next[N][M],fail[N],end[N];
    int root,L;
    int newnode()
    {
        for(int i = 0;i < M;i++)//每一个节点对应0-128中的任意一个。
            next[L][i] = -1;
        end[L++] = -1;//表示下面没有节点 初始化,如果是记录次数,就赋0 还可以赋任意的数,
        return L-1;
    }
    void init()
    {
        L = 0;
        root = newnode();
    }
    void insert(char s[])
    {
        int len = strlen(s);
        int now = root;
        for(int i = 0;i < len;i++)
        {
            if(next[now][s[i]-'A'] == -1)
                next[now][s[i]-'A'] = newnode();
            now=next[now][s[i]-'A'];//记录其对应的节点编号
        }
        end[now]=1;//记录当前匹配单词的节点
        //end[now]++;也可以用匹配单词结束后来记录次数
    }
    void build()
    {
        queue<int>Q;
        fail[root] = root;//根节点仍然是根节点
        for(int i = 0;i < M;i++)//对第一个字符遍历
            if(next[root][i] == -1)//没有此字符开头
                next[root][i] = root;//跳转到根
            else//有此字符开头的
            {
                fail[next[root][i]] = root;//这个行位的失败指针为根
                Q.push(next[root][i]);//行放入队列
            }
        while(!Q.empty())//还有字符
        {
            int now = Q.front();//逐层拿出第一个
            Q.pop();
            if(end[fail[now]]== 1) end[now] = 1;
            for(int i = 0;i < M;i++)//对这一行
                if(next[now][i] == -1)//如果下一行没有这个字符
                    next[now][i] = next[fail[now]][i];//他的下一个的这
                else//如果有这个字符
                {
                    fail[next[now][i]] = next[fail[now]][i];//他的下一个的
                    Q.push(next[now][i]);//下一行继续
                }
        }
    }


    /*bool used[N];//判断其是否被查找到
    bool query(char buf[],int n,int id)
    {
        int len = strlen(buf);
        int now = root;
        memset(used,false,sizeof(used));//初始化used
        bool flag = false;
        for(int i = 0;i < len;i++)
        {
            now = next[now][buf[i]];
            int temp = now;
            while(temp != root)
            {
                if(end[temp] != -1)
                {
                    used[end[temp]] = true;//记录被匹配的信息
                    flag = true;
                }
                temp = fail[temp];
            }
        }

    }*/
    Matrix getMatrix()
    {
        Matrix ret = Matrix(L);
        memset(ret.mat,0,sizeof(ret.mat));
        for(int i = 0;i < L;i++)
        {
            if(end[i]==1) continue;
            for(int j = 0;j < 26;j++)
            {
                if(end[next[i][j]]==-1)
                    ret.mat[i][next[i][j]] ++;
            }
        }
        return ret;
    }
};

Trie ac;
char buf[20];
int main()
{
    int m;
    int n;
    while(scanf("%d%d",&m,&n) != EOF)
    {
        ac.init();
        for(int i = 0; i < m; i++)
        {
            scanf("%s",buf);
            ac.insert(buf);
        }
        ac.build();
        Matrix ta = ac.getMatrix();
        int ans = 0;
        ta = pow_mat(ta,n);
        for(int i = 0;i < ta.len;i++)
        {
            ans = ans+(int)ta.mat[0][i];
        }
        ans = qpow(26,n)-ans;
        while(ans < 0)
            ans += MOD;
        printf("%d\n",ans%MOD);
    }
    return 0;
}





  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值