本文主要是介绍BZOJ 3530 数数【AC自动机+数位dp】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
[Sdoi2014]数数
简单数位dp+简单AC自动机
反正数位DP是队友写的
AC自动机要记录两个值,一个是是否为一个串的结束,即不合法状态,一个是前缀零的情况。
// whn6325689
// Mr.Phoebe
// http://blog.csdn.net/u013007900
#include <algorithm>
#include <iostream>
#include <iomanip>
#include <cstring>
#include <climits>
#include <complex>
#include <fstream>
#include <cassert>
#include <cstdio>
#include <bitset>
#include <vector>
#include <deque>
#include <queue>
#include <stack>
#include <ctime>
#include <set>
#include <map>
#include <cmath>
#include <functional>
#include <numeric>
#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;
#define eps 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LLINF 1LL<<50
#define speed std::ios::sync_with_stdio(false);typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef complex<ld> point;
typedef pair<int, int> pii;
typedef pair<pii, int> piii;
typedef vector<int> vi;#define CLR(x,y) memset(x,y,sizeof(x))
#define CPY(x,y) memcpy(x,y,sizeof(x))
#define clr(a,x,size) memset(a,x,sizeof(a[0])*(size))
#define cpy(a,x,size) memcpy(a,x,sizeof(a[0])*(size))
#define debug(a) cout << #a" = " << (a) << endl;
#define debugarry(a, n) for (int i = 0; i < (n); i++) { cout << #a"[" << i << "] = " << (a)[i] << endl; }#define mp(x,y) make_pair(x,y)
#define pb(x) push_back(x)
#define lowbit(x) (x&(-x))#define MID(x,y) (x+((y-x)>>1))
#define getidx(l,r) (l+r | l!=r)
#define ls getidx(l,mid)
#define rs getidx(mid+1,r)
#define lson l,mid
#define rson mid+1,rtemplate<class T>
inline bool read(T &n)
{T x = 0, tmp = 1;char c = getchar();while((c < '0' || c > '9') && c != '-' && c != EOF) c = getchar();if(c == EOF) return false;if(c == '-') c = getchar(), tmp = -1;while(c >= '0' && c <= '9') x *= 10, x += (c - '0'),c = getchar();n = x*tmp;return true;
}
template <class T>
inline void write(T n)
{if(n < 0){putchar('-');n = -n;}int len = 0,data[20];while(n){data[len++] = n%10;n /= 10;}if(!len) data[len++] = 0;while(len--) putchar(data[len]+48);
}
//-----------------------------------const int MAXN=2010;
const int MOD=1e9+7;
const int MAC=10;int dp[MAXN][MAXN][2];
int n,m,a[MAXN];
char s[MAXN];struct AC
{int ch[MAXN][MAC],f[MAXN],sz;int end[MAXN],zero[MAXN],fa[MAXN];void init(){sz=f[0]=0;newnode(0);}int newnode(int pre){CLR(ch[sz],0);fa[sz]=pre;end[sz]=zero[sz]=0;return sz++;}void insert(char *S){int u=0,x;int n=strlen(S);for(int i=n-1;i>=0;i--){x=S[i]-'0';if(!ch[u][x]) ch[u][x]=newnode(u);u=ch[u][x];}end[u]=1;zero[u]=S[0]=='0';}int que[MAXN],front,back;void getfail(){int u,cur,ans=-1;front=back=0;for(int i=0;i<MAC;i++)if(ch[0][i]){que[back++]=ch[0][i];f[ch[0][i]]=0;}while(front<back){cur=que[front++];for(int i=0;i<MAC;i++){u=ch[cur][i];if(u){que[back++]=u;f[u]=ch[f[cur]][i];}elsech[cur][i]=ch[f[cur]][i];end[u] |= end[f[u]];}} }void DP(int u,int cur,int over){int &ret=dp[u][cur][over];if(~ret) return ;if(end[u]){int t=0;if(zero[u]) t=1;dp[u][cur][0]=dp[u][cur][1]=t;if(!cur) dp[u][cur][1]=0;return;}if(!cur){dp[u][cur][1]=0;dp[u][cur][0]=1;return;}ret=0;for(int c=0; c<MAC; c++){if(over){if(c>=a[cur-1]){DP(ch[u][c],cur-1,1);ret=(ret+dp[ch[u][c]][cur-1][1]) % MOD;}else{DP(ch[u][c],cur-1,0);ret=(ret+dp[ch[u][c]][cur-1][0]) % MOD;}}else{if(c>a[cur-1]){DP(ch[u][c],cur-1, 1);ret=(ret+dp[ch[u][c]][cur-1][1]) % MOD;}else{DP(ch[u][c],cur-1, 0);ret=(ret+dp[ch[u][c]][cur-1][0]) % MOD;}}}}
} ac;int main()
{//freopen("data.txt","r",stdin);ac.init(); CLR(dp,-1);scanf("%s",s);n=strlen(s);for(int i=0; i<n; i++) a[i]=s[i]-'0';scanf("%d",&m);for(int i=1; i<=m; i++){scanf("%s",s);ac.insert(s);}ac.getfail();ac.DP(0,n,0);printf("%d\n", dp[0][n][0]-1);return 0;
}
这篇关于BZOJ 3530 数数【AC自动机+数位dp】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!