zoukankan      html  css  js  c++  java
  • bzoj3530 [SDOI2014]数数

    题目链接

    solution

    容易想到将集合中的所有串建出AC自动机。然后用(f[i][j][0/1])表示前(i)个位置是(1)否(0)为上界,第(i)个位置对应AC自动机中的(j)号点的方案数。

    转移就枚举当前位置填的数字转移即可。

    有两个需要注意的地方:

    • 如果某个位置可以通过fail指针跳到一个子串的结束节点,那么这个节点也应该被标记为结束节点。
    • 需要排除前导零的情况。可以枚举数字的起始节点来达到这个目的。

    code

    /*
    * @Author: wxyww
    * @Date:   2020-04-19 08:45:06
    * @Last Modified time: 2020-04-19 10:07:21
    */
    #include<cstdio>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    #include<vector>
    #include<ctime>
    using namespace std;
    typedef long long ll;
    const int N = 3010,mod = 1e9 + 7;
    ll read() {
    	ll x = 0,f = 1;char c = getchar();
    	while(c < '0' || c > '9') {
    		if(c == '-') f = -1; c = getchar();
    	}
    	while(c >= '0' && c <= '9') {
    		x = x * 10 + c - '0'; c = getchar();
    	}
    	return x * f;
    }
    int trie[N][11],fail[N];
    char s[N],t[N];
    
    int f[N][N][2],bz[N],tot;
    void insert() {
    	int m = strlen(t + 1);
    	int now = 0;
    	for(int i = 1;i <= m;++i) {
    		int x = t[i] - '0';
    		if(!trie[now][x]) trie[now][x] = ++tot;
    		now = trie[now][x];
    	}
    	bz[now] = 1;
    }
    queue<int>q;
    void build() {
    	for(int i = 0;i < 10;++i) if(trie[0][i]) q.push(trie[0][i]);
    	while(!q.empty()) {
    		int u = q.front();q.pop();
    		for(int i = 0;i < 10;++i) {
    			if(!trie[u][i]) trie[u][i] = trie[fail[u]][i];
    			else {
    				if(bz[trie[fail[u]][i]]) bz[trie[u][i]] = 1;
    				fail[trie[u][i]] = trie[fail[u]][i],q.push(trie[u][i]);
    			}
    		}
    	}
    }
    int n;
    void upd(int &x,int y) {
    	x += y;x >= mod ? x -= mod : 0;
    }
    void dfs(int pos) {
    	if(pos == n) return;
    
    	for(int j = 1;j < 10;++j) {
    		if(bz[trie[0][j]]) continue;
    			upd(f[pos + 1][trie[0][j]][0],1);
    	}
    	for(int i = 0;i <= tot;++i) {
    		if(bz[i]) continue;
    
    		for(int j = 0;j <= 9;++j) {
    			upd(f[pos + 1][trie[i][j]][0] ,f[pos][i][0]);
    		}
    		
    		for(int j = 0;j <= s[pos + 1] - '0';++j) {
    
    			upd(f[pos + 1][trie[i][j]][j == (s[pos + 1] - '0')] , f[pos][i][1]);
    		}
    
    	}
    	dfs(pos + 1);
    }
    
    int main() {
    	// freopen("3530/4.in","r",stdin);
    	scanf("%s",s + 1);
    	n = strlen(s + 1);
    	int m = read();
    	while(m--) {
    		scanf("%s",t + 1);
    		insert();
    	}
    	build();
    
    	for(int i = 1;i < s[1] - '0';++i) f[1][trie[0][i]][0]++;
    	f[1][trie[0][s[1] - '0']][1]++;
    
    	dfs(1);
    
    	int ans = 0;
    	for(int i = 0;i <= tot;++i) {
    		if(bz[i]) continue;
    		upd(ans,(f[n][i][0] + f[n][i][1]) % mod);
    	}
    	cout<<ans % mod<<endl;
    	return 0;
    }
    
  • 相关阅读:
    SSRF
    【Floyd(并非水题orz)】BZOJ4093-[Usaco2013 Dec]Vacation Planning
    【SPFA+二分答案】BZOJ1614- [Usaco2007 Jan]Telephone Lines架设电话线
    【刷水-贪心】BZOJ1629-[Usaco2007 Demo]Cow Acrobats
    【刷水-二分答案】BZOJ1650 & BZOJ1639
    【UOJ244】[UER7]短路
    【尺取法好题】POJ2566-Bound Found
    【尺取法】POJ3061 & POJ3320
    【hash】BZOJ3751-[NOIP2014]解方程
    【最小点覆盖】POJ3041-Asteroids
  • 原文地址:https://www.cnblogs.com/wxyww/p/bzoj3530.html
Copyright © 2011-2022 走看看