zoukankan      html  css  js  c++  java
  • ZOJ 3494 BCD Code (*AC自动机+数位DP 待整理)

    Binary-coded decimal (BCD) is an encoding for decimal numbers in which each digit is represented by its own binary sequence. To encode a decimal number using the common BCD encoding, each decimal digit is stored in a 4-bit nibble:

    Decimal:    0     1     2     3     4     5     6     7     8     9
    BCD:     0000  0001  0010  0011  0100  0101  0110  0111  1000  1001
    

    Thus, the BCD encoding for the number 127 would be:

     0001 0010 0111
    

    We are going to transfer all the integers from A to B, both inclusive, with BCD codes. But we find that some continuous bits, named forbidden code, may lead to errors. If the encoding of some integer contains these forbidden codes, the integer can not be transferred correctly. Now we need your help to calculate how many integers can be transferred correctly.

    Input

    There are multiple test cases. The first line of input is an integer T ≈ 100 indicating the number of test cases.

    The first line of each test case contains one integer N, the number of forbidden codes ( 0 ≤ N ≤ 100). Then N lines follow, each of which contains a 0-1 string whose length is no more than 20. The next line contains two positive integers A and B. Neither A or B contains leading zeros and 0 < A ≤ B < 10200.

    <b< dd="">

    Output

    For each test case, output the number of integers between A and B whose codes do not contain any of the N forbidden codes in their BCD codes. For the result may be very large, you just need to output it mod 1000000009.

    <b< dd="">

    Sample Input

    3
    1
    00
    1 10
    1
    00
    1 100
    1
    1111
    1 100
    

    <b< dd="">

    Sample Output

    3
    9
    98
    

    References

    Hint


    首先使用AC自动机,得到bcd[i][j]表示状态i,加了数字j以后到达的状态,为-1表示不能转移。(用AC自动机预处理出从一个状态的一个数转移到下一个数时的状态

    然后就是数位DP了

     

    注意记录为0的状态



    先贴一下kuangbin的代码,回头整理!!


    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <queue>
    using namespace std;
    
    struct Trie
    {
        int next[2010][2],fail[2010];
        bool end[2010];
        int root,L;
        int newnode()
        {
            for(int i = 0;i < 2;i++)
                next[L][i] = -1;
            end[L++] = false;
            return L-1;
        }
        void init()
        {
            L = 0;
            root = newnode();
        }
        void insert(char buf[])
        {
            int len = strlen(buf);
            int now = root;
            for(int i = 0;i < len ;i++)
            {
                if(next[now][buf[i]-'0'] == -1)
                    next[now][buf[i]-'0'] = newnode();
                now = next[now][buf[i]-'0'];
            }
            end[now] = true;
        }
        void build()
        {
            queue<int>Q;
            fail[root] = root;
            for(int i = 0;i < 2;i++)
                if(next[root][i] == -1)
                    next[root][i] = root;
                else
                {
                    fail[next[root][i]] = root;
                    Q.push(next[root][i]);
                }
            while(!Q.empty())
            {
                int now = Q.front();
                Q.pop();
                if(end[fail[now]])end[now] = true;
                for(int i = 0;i < 2;i++)
                    if(next[now][i] == -1)
                        next[now][i] = next[fail[now]][i];
                    else
                    {
                        fail[next[now][i]] = next[fail[now]][i];
                        Q.push(next[now][i]);
                    }
            }
        }
    };
    Trie ac;
    
    int bcd[2010][10];
    int change(int pre,int num)
    {
        if(ac.end[pre])return -1;
        int cur = pre;
        for(int i = 3;i >= 0;i--)
        {
            if(ac.end[ac.next[cur][(num>>i)&1]])return -1;
            cur = ac.next[cur][(num>>i)&1];
        }
        return cur;
    }
    void pre_init()
    {
        for(int i = 0;i <ac.L;i++)
            for(int j = 0;j <10;j++)
                bcd[i][j] = change(i,j);
    }
    const int MOD = 1000000009;
    long long dp[210][2010];
    int bit[210];
    
    long long dfs(int pos,int s,bool flag,bool z)
    {
        if(pos == -1)return 1;
        if(!flag && dp[pos][s]!=-1)return dp[pos][s];
        
        
        long long ans = 0;
        if(z)
            ans += dfs(pos-1,s,flag && bit[pos]==0,true),ans %= MOD;
        else
        {
            if(bcd[s][0]!=-1)
                ans += dfs(pos-1,bcd[s][0],flag && bit[pos]==0,false);
            ans %= MOD;
        }
        
        
        int end = flag?bit[pos]:9;
        for(int i = 1;i<=end;i++)
        {
            if(bcd[s][i]!=-1)
                ans += dfs(pos-1,bcd[s][i],flag&&i==end,false),ans %=MOD;
        }
        
        
        if(!flag && !z)dp[pos][s] = ans;
        return ans;
    }
    
    long long calc(char s[])
    {
        int len = strlen(s);
        for(int i = 0;i < len;i++)
            bit[i] = s[len-1-i]-'0';
        return dfs(len-1,0,1,1);
    }
    char str[210];
    int main()
    {
    //    freopen("in.txt","r",stdin);
    //    freopen("out.txt","w",stdout);
        int T;
        scanf("%d",&T);
        int n;
        while(T--)
        {
            ac.init();
            scanf("%d",&n);
            for(int i = 0;i < n;i++)
            {
                scanf("%s",str);
                ac.insert(str);
            }
            ac.build();
            pre_init();
            memset(dp,-1,sizeof(dp));
            int ans = 0;
            scanf("%s",str);
            int len = strlen(str);
            for(int i = len -1;i >=0;i--)
            {
                if(str[i]>'0')
                {
                    str[i]--;
                    break;
                }
                else str[i] = '9';
            }
            ans -= calc(str);
            ans %=MOD;
            scanf("%s",str);
            ans += calc(str);
            ans %=MOD;
            if(ans < 0)ans += MOD;
            printf("%d
    ",ans);
        }
        return 0;
    }


  • 相关阅读:
    MyBatis的初始化方式
    WCF X.509验证
    NPOI导出EXCEL 打印设置分页及打印标题
    一些旁门左道
    曲线救国:IIS7集成模式下如何获取网站的URL
    图片的粘贴上传
    让EF飞一会儿:如何用Entity Framework 6 连接Sqlite数据库
    ASP.NET 保存txt文件
    JavaScript高级程序设计学习笔记--高级技巧
    JavaScript高级程序设计学习笔记--错误处理与调试
  • 原文地址:https://www.cnblogs.com/zswbky/p/6792869.html
Copyright © 2011-2022 走看看