hdu 2243 考研路茫茫——单词情结(2013华师校赛 H题 Choose a password)

链接: http://acm.hdu.edu.cn/showproblem.php?pid=2243

http://acm.hdu.edu.cn/diy/contest_showproblem.php?pid=1008&cid=19449


ac自动机+矩阵快速幂求和

入门:http://poj.org/problem?id=2778   ;  http://poj.org/problem?id=3233


x + x^2 + x^3 + x^4 + .... + x^n = x^(n/2)*(x + x^2 + x^3 + ... + x^(n/2)) + (n&1)? x^n: 0;



#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<string>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
using namespace std;
#define M 27
#define N 107
#define ll unsigned long long
#define mod 1000000000
#define inf 1<<30


struct AC
{
    int son[M][26], flag[M], fail[M], id[M];
    int t, root;

    int newNode(){
        ++t;
        memset( son[t], 0, sizeof(son[t]) );
        flag[t] = fail[t] = 0;
        id[t] = t;
        return t;
    }
    void init(){
        t = -1;
        root = newNode();
    }

    int get( char c ){
        return c - 'a';
        /*
        switch( c ){
            case 'A': return 0;
            case 'T': return 1;
            case 'C': return 2;
            case 'G': return 3;
        }
        return -1;
        */
    }
    void insert( char *str ){
        int p = root, x;
        for( int i = 0; str[i]; ++i ){
            x = get( str[i] );
            if( !son[p][x] ) son[p][x] = newNode();
            p = son[p][x];
        }
        flag[p] = 1;
    }
    void setFail( ll a[][M] )
    {
        for( int i = 0; i <= t; ++i )
            for( int j = 0; j <= t; ++j ) a[i][j] = 0;

        queue<int> q;
        int p, x, fa;
        q.push( root );
        while( !q.empty() ){
            p = q.front(); q.pop();
            for( int i = 0; i < 26; ++i ){
                x = son[p][i];
                if( p == root ){
                    if( x ){
                        q.push( x );
                        fail[x] = root;
                    }
                }
                else{
                    fa = fail[p];
                    if( x ){
                        q.push( x );

                        while( fa && !son[fa][i] ) fa = fail[fa];
                        fail[x] = son[fa][i];
                        if( flag[fail[x]] )
                            flag[x] = 1;
                    }
                    else{
                        son[p][i] = son[fa][i];
                    }
                }
                x = son[p][i];
                if( !flag[x] )
                    a[id[p]][id[x]] += 1;
            }
        }
    }
}ac;

struct Matrix
{
    ll a[M][M];
    int n;
    Matrix(){
        this->n = n;
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j ) a[i][j] = ( i==j );
    }
    Matrix( ll b[][M], int n ){
        this->n = n;
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j ) this->a[i][j] = b[i][j];
    }
    void init(){
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j ) a[i][j] = ( i==j );
    }
    Matrix operator * ( const Matrix &op ) const{
        ll c[M][M];
        memset( c, 0, sizeof(c) );
        for( int k = 0; k < n; ++k )
        for( int i = 0; i < n; ++i ) if( this->a[i][k] ){
            for( int j = 0; j < n; ++j ) if( op.a[k][j] ){
                c[i][j] += this->a[i][k]*op.a[k][j];
                //c[i][j] %= mod;
            }
        }
        return Matrix( c, n );
    }
    Matrix operator + ( const Matrix &op ) const{
        ll c[M][M];
        for( int i = 0; i < n; ++i )
            for( int j = 0; j < n; ++j )
                c[i][j] = ( this->a[i][j] + op.a[i][j] ) ;
        return Matrix( c, n );
    }
}mat;
Matrix dfs1( Matrix , int  );
Matrix powMod( Matrix , int );
Matrix dfs( Matrix a, int n )
{
    if( n == 1 ) return a;
    Matrix t = dfs( a, n/2 );
    Matrix b = dfs1( a, n/2 );
    t = t + t*b;
    if( n&1 ) t = t + dfs1( a, n );
    return t;
}
Matrix dfs1( Matrix a, int n )
{
    if( n == 1 ) return a;
    Matrix t = dfs1( a, n/2 );
    t = t*t;
    if( n&1 ) t = t * a;
    return t;
}

Matrix powMod( Matrix a, int n )
{
    Matrix b;
    b.n = a.n;
    b.init();
    while( n ){
        if( n&1 ) b = b*a;
        a = a*a;
        n >>= 1;
    }
    return b;
}
ll dfs1( ll, int );
ll dfs( ll a, int n )
{
    if( n == 1 ) return a;
    ll t = dfs( a, n/2 );
    ll b = dfs1( a, n/2 );
    t = t*b + t;
    if( n&1 ) t += dfs1( a, n );
    return t;
}
ll dfs1( ll a, int n )
{
    if( n == 1 ) return a;
    ll t = dfs1( a, n/2 );
    t *= t;
    if( n&1 ) t *= a;
    return t;
}
char str[30];
ll a[M][M];

int main()
{
    //cout<<(1<<31)-1<<endl;
    #ifndef ONLINE_JUDGE
        freopen( "a.in", "r", stdin );
    #endif // ONLINE_JUDGE
    int n, m, k;
    while( scanf( "%d%d", &n, &m ) == 2 ){
        ac.init();
        for( int i = 0; i < n; ++i ){
            scanf( "%s", str );
            ac.insert( str );
        }
        ac.setFail( a );
        mat = Matrix( a, ac.t+1 );
        mat = dfs( mat, m );
        ll ans = 0;
        for( int i = 0; i < mat.n; ++i )
            ans += mat.a[0][i];
        ans = dfs( 26LL, m ) - ans;
        cout<<ans<<endl;
    }
}

ps: M = 30 会爆栈, M = 25会WA, M = 27 AC

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值