1. 程式人生 > >Bzoj3530: [Sdoi2014]數數

Bzoj3530: [Sdoi2014]數數

lin sin cnblogs 自動 || www 註意 define else

題面

傳送門

Sol

在AC自動機上跑數位DP
\(f[i][j][0/1]\)表示到\(n的第i位\)當前匹配到\(AC自動機的j節點\)的方案
轉移就在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; }

Bzoj3530: [Sdoi2014]數數