「BZOJ1030」[JSOI2007] 文本生成器

 AC自动机上dp

solution 1 :

按照字符串的长度进行dp的(字符串的长度放在最外层)

#include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
const int N = 6005;
int ch[N][26],tot,f[N],cnt[N],val[N],last[N];
void init(int n){
    tot = 0;
    memset( ch[0],0,sizeof( ch[0] ) );
}
void Insert( char* str ){
    int p = 0;int n = strlen( str );
    for( int i = 0;i < n;i++ ){
        int c = str[i]-'A';
        if( !ch[p][c] )  {
            ch[p][c] = ++tot;val[tot]= 0;last[tot] = 0;
            //memset( ch[tot],0,sizeof( ch[tot] ) );
        }
        p = ch[p][c];
    }
    val[p] = 1;
}
int que[N];
void getfail(){
    int l = 1,r = 0;
    for( int i = 0;i < 26;i++ ){
        if( ch[0][i] ) que[++r] = ch[0][i];
    }
    while(l<=r){
        int x = que[l++];
        for( int c = 0;c < 26;c++ ){
            if( !ch[x][c] ){
                ch[x][c] = ch[ f[x] ][c];
                continue;
            }
            f[ ch[x][c] ] = ch[ f[x] ][c];
            last[ch[x][c]] = val[ f[ ch[x][c] ] ] ? f[ch[x][c]] : last[ f[ ch[x][c] ] ];
            que[++r] = ch[x][c];
        }
    }
}
char str[maxn];
const int mod = 10007;
int dp[N][maxn],ans,m;
void solve(){
    dp[0][0] = 1;
    for( int j = 0;j <= m;j++ )
    for( int i = 0;i <= tot;i++ ){
            if( val[i] || val[ last[i] ] || !dp[i][j] ) continue;
            for( int k = 0;k < 26;k++){
                int y = ch[i][k];
                if( val[ y ] || val[ last[y] ] ) continue;
                dp[y][j+1] = (dp[y][j+1]+dp[i][j])%mod;
            }
    }
}
int main(){
    int n;
    scanf("%d%d",&n,&m);
    for( int i = 1;i <= n;i++ ){
        scanf("%s",str);
        Insert(str);
    }
    getfail();
    dp[0][0] = 1;
    solve();
    ans = 1;
    for( int i = 1;i <= m;i++ ) ans = ans*26%mod;
    for( int i = 0;i <= tot;i++ ){
            ans = (ans-dp[i][m]+mod)%mod;
    }
    printf("%d\n",ans);
    return 0;
}

solution 2

#include <bits/stdc++.h>
using namespace std;
const int mod = 10007;
const int maxn = 105;
const int N = 10005;
int ch[N][26],tot,f[N],val[N],last[N];
void init(){
    tot = 0;
    memset( ch[0],0,sizeof(ch[0]) );
}
void Insert( char* str ){
    int p = 0;int  n =strlen(str);
    for( int i = 0;i < n;i++ ){
        int c= str[i]-'A';
        if( !ch[p][c] ){
            ch[p][c] = ++tot;val[tot] = 0;last[tot] = 0;f[tot] = 0;
            memset( ch[tot],0,sizeof( ch[tot] ) );
        }
        p = ch[p][c];
    }
    val[p] = 1;
}
queue<int> que;
void getfail(){
    for( int i = 0;i < 26;i++ ){
        if( ch[0][i] ) que.push( ch[0][i] );
    }
    while( que.size() ){
        int x = que.front();
        que.pop();
        for( int c = 0;c < 26;c++ ){
            if(!ch[x][c] ){
                ch[x][c] = ch[ f[x] ][c];
                continue;
            }
            f[ ch[x][c] ] = ch[ f[x] ][c];
            last[ ch[x][c] ] = val[ f[ ch[x][c] ] ] ? f[ ch[x][c] ]:last[ f[ ch[x][c] ] ];
            que.push( ch[x][c] );
        }
    }
}
char str[maxn];
int dp[N][maxn],m;
int dfs( int x,int len ){
    if( dp[x][len] != -1 ) return dp[x][len];
    if( val[x] || val[ last[x] ] ){
        return dp[x][len] = 0;
    }
    if( len == m ) return dp[x][len] = 1;
    int res = 0;
    for( int c = 0;c < 26;c++ ){
        int y = ch[x][c];
        res += dfs(y,len+1);
        res %= mod;
    }
    return dp[x][len] = res;
}
int main()
{
    memset( dp,-1,sizeof(dp) );
    int n;
    scanf("%d%d",&n,&m);
    for( int i = 1;i <= n;i++ ){
        scanf("%s",str);
        Insert(str);
    }
    getfail();
    int res = dfs( 0,0 );
    int sum = 1;
    for( int i = 1;i <= m;i++ ){
        sum *= 26;
        sum %= mod;
    }
    sum = (sum-res+mod)%mod;
    printf("%d\n",sum);
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值