Bzoj3530: [Sdoi2014]数数

题面

传送门

Sol

在AC自动机上跑数位DP
f[i][j][0/1] 表示到 ni 当前匹配到 ACj 的方案
转移就在AC自动机上跑
注意不能有前导零,可能有这种情况 000000 不能存在那么前导零就有问题
所以要单独把小于 n 的位数的数单独算出来,等于n的位数的数单独算出来最后加起来

# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int Zsy(1e9 + 7), _(1510);

int n, m, ch[10][_], tot, fail[_], f[3][_][_], ans;
bool cnt[_];
char s[_], T[_];
queue <int> Q;

IL void Insert(){
    RG int x = 0, len = strlen(s);
    for(RG int i = 0; i < len; ++i){
        if(!ch[s[i] - '0'][x]) ch[s[i] - '0'][x] = ++tot;
        x = ch[s[i] - '0'][x];
    }
    cnt[x] = 1;
}

IL void GetFail(){
    for(RG int i = 0; i <= 9; ++i) if(ch[i][0]) Q.push(ch[i][0]);
    while(!Q.empty()){
        RG int fa = Q.front(); Q.pop();
        for(RG int i = 0; i <= 9; ++i)
            if(ch[i][fa]) fail[ch[i][fa]] = ch[i][fail[fa]], Q.push(ch[i][fa]);
            else ch[i][fa] = ch[i][fail[fa]];
        cnt[fa] |= cnt[fail[fa]];
    }
}

IL void Calc(){
    f[1][0][0] = f[2][0][0] = 1;
    for(RG int i = 0; i < n; ++i)
        for(RG int j = 0; j <= tot; ++j){
            if(cnt[j]) continue;
            for(RG int l = 0; l <= 9; ++l){
                RG int p = ch[l][j];
                if(!(i + l) || cnt[p]) continue;
                (f[2][i + 1][p] += f[2][i][j]) %= Zsy;
            }
        }
    for(RG int i = 1; i < n; ++i)
        for(RG int j = 0; j <= tot; ++j)
            (ans += f[2][i][j]) %= Zsy;
    for(RG int i = 0; i < n; ++i)
        for(RG int j = 0; j <= tot; ++j){
            if(cnt[j]) continue;
            for(RG int l = 0; l <= 9; ++l){
                RG int p = ch[l][j];
                if(!(i + l) || cnt[p]) continue;
                (f[0][i + 1][p] += f[0][i][j]) %= Zsy;
                if(l + '0' == T[i + 1]) (f[1][i + 1][p] += f[1][i][j] % Zsy) %= Zsy;
                if(l + '0' < T[i + 1]) (f[0][i + 1][p] += f[1][i][j]) %= Zsy;
            }
        }
}

int main(RG int argc, RG char* argv[]){
    scanf(" %s%d", T + 1, &m); n = strlen(T + 1);
    for(RG int i = 1; i <= m; ++i) scanf(" %s", s), Insert();
    GetFail(); Calc();
    for(RG int i = 0; i <= tot; ++i) (ans += (f[0][n][i] + f[1][n][i]) % Zsy) %= Zsy;
    printf("%d\n", ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值