ZOJ3697-Bad-written Number

100 篇文章 0 订阅
66 篇文章 0 订阅

Bad-written Number

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Description

A little kid loves to write digits in LC display style, but he would like to put two consecutive digits very close to each other. Sometimes, that makes the number he writes ambiguous.

In LC display style, each digit is written as below:

0123456789
 _ 
| |
|_|

   
  |
  |

 _ 
 _|
|_ 

 _ 
 _|
 _|

|_|
  |

 _ 
|_ 
 _|

 _ 
|_ 
|_|

 _ 
  |
  |

 _ 
|_|
|_|

 _ 
|_|
 _|

As the table shown above, in LC display style, each digit is written in 3 rows and 3 columns. The little kid always overlaps the last column of the previous digit and the first column of the next digit.

Please help his parents to count the number of different ways to translate the weird 'image'.

Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 1500) indicating the number of test cases. Then T test cases follow.

The first line of each case contains one integer: n (n ≤ 104) -- the number of digits that the little kid writes.

Then each of the following 3 lines contains exactly 2n+1 characters -- represents the number written by the kid.

Output
For each test case, print a single number -- the number of ways to express the bad-written 'image' module 10 9 +7 (which is equivalent to print  answer % 1000000007 , where  %  is the module operator in all major programming languages).
Sample Input
6
1
   
|  
|  
1
 _ 
|_|
|_ 
1

  |
  |
1
 _
|_|
|_|
2
   _ 
  |_|
  |_|
2
 _ _ 
 _|_ 
|_ _|
Sample Output
0
0
1
1
3
1
Hint
Sometimes, the little kid may make some mistakes, in which case you should output 0. It's forbidden to miss anything or change the character's position (it should be exactly the same as the table shown above). So the answers of the first two cases are 0s. The third case can be translated in 3 ways: 13, 18, 19.
Author:  OUYANG, Jialin
Contest:  The 13th Zhejiang University Programming Contest


题意:用上所有的边,问有几种组成数字的可能,边可以重叠

解题思路:一个数单独判断。第一个和最后一个单独判断一下,其余的根据中间来判断,当两个相邻的刚好满足中间竖线的情况,则dp[i][k]+=dp[i-1][kk]


#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <stack>
#include <map>
#include <cctype>
#include <sstream>
#include <climits>

using namespace std;

#define LL long long
const int INF=0x3f3f3f3f;

char ch[3][30009];
int a[10009][15];
LL dp[10009][15];
int n;

const LL mod=1e9+7;

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        memset(a,0,sizeof a);
        getchar();
        for(int i=0; i<3; i++)
            gets(ch[i]);
        int len=2*n+1;
        int p;
        for(p=0;ch[0][p];p++) ;
        for(int i=p;i<len;i++)
            ch[0][i]=' ';
        ch[0][len]='\0';
        for(p=0;ch[1][p];p++) ;
        for(int i=p;i<len;i++)
            ch[1][i]=' ';
        ch[1][len]='\0';
        for(p=0;ch[2][p];p++) ;
        for(int i=p;i<len;i++)
            ch[2][i]=' ';
        ch[2][len]='\0';
        if(n==1)
        {
            int flag=0;
            if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]==' '&&ch[2][0]=='|'&&ch[2][1]=='_'&&
               ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') flag=1;
            if(ch[0][0]==' '&&ch[0][1]==' '&&ch[1][0]==' '&&ch[1][1]==' '&&ch[2][0]==' '&&ch[2][1]==' '&&
               ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') flag=1;
            if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]==' '&&ch[1][1]=='_'&&ch[2][0]=='|'&&ch[2][1]=='_'&&
               ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]==' ') flag=1;
            if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]==' '&&ch[1][1]=='_'&&ch[2][0]==' '&&ch[2][1]=='_'&&
               ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') flag=1;
            if(ch[0][0]==' '&&ch[0][1]==' '&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]==' '&&ch[2][1]==' '&&
               ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') flag=1;
            if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]==' '&&ch[2][1]=='_'&&
               ch[0][2]==' '&&ch[1][2]==' '&&ch[2][2]=='|') flag=1;
            if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]=='|'&&ch[2][1]=='_'&&
               ch[0][2]==' '&&ch[1][2]==' '&&ch[2][2]=='|') flag=1;
            if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]==' '&&ch[1][1]==' '&&ch[2][0]==' '&&ch[2][1]==' '&&
               ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') flag=1;
            if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]=='|'&&ch[2][1]=='_'&&
               ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') flag=1;
            if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]==' '&&ch[2][1]=='_'&&
               ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') flag=1;
            printf("%d\n",flag);continue;
        }
        if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]==' '&&ch[2][0]=='|'&&ch[2][1]=='_'&&
           ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') a[1][0]=1;
        if(ch[0][0]==' '&&ch[0][1]==' '&&ch[1][0]==' '&&ch[1][1]==' '&&ch[2][0]==' '&&ch[2][1]==' '&&
           ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') a[1][1]=1;
        if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]==' '&&ch[1][1]=='_'&&ch[2][0]=='|'&&ch[2][1]=='_'&&
           ch[0][2]==' '&&ch[1][2]=='|') a[1][2]=1;
        if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]==' '&&ch[1][1]=='_'&&ch[2][0]==' '&&ch[2][1]=='_'&&
           ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') a[1][3]=1;
        if(ch[0][0]==' '&&ch[0][1]==' '&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]==' '&&ch[2][1]==' '&&
           ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') a[1][4]=1;
        if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]==' '&&ch[2][1]=='_'&&
           ch[0][2]==' '&&ch[2][2]=='|') a[1][5]=1;
        if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]=='|'&&ch[2][1]=='_'&&
           ch[0][2]==' '&&ch[2][2]=='|') a[1][6]=1;
        if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]==' '&&ch[1][1]==' '&&ch[2][0]==' '&&ch[2][1]==' '&&
           ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') a[1][7]=1;
        if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]=='|'&&ch[2][1]=='_'&&
           ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') a[1][8]=1;
        if(ch[0][0]==' '&&ch[0][1]=='_'&&ch[1][0]=='|'&&ch[1][1]=='_'&&ch[2][0]==' '&&ch[2][1]=='_'&&
           ch[0][2]==' '&&ch[1][2]=='|'&&ch[2][2]=='|') a[1][9]=1;

        if(ch[0][len-3]==' '&&ch[0][len-2]=='_'&&ch[1][len-3]=='|'&&ch[1][len-2]==' '&&ch[2][len-3]=='|'&&
           ch[2][len-2]=='_'&&ch[0][len-1]==' '&&ch[1][len-1]=='|'&&ch[2][len-1]=='|') a[n][0]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]==' '&&ch[1][len-2]==' '&&ch[2][len-2]==' '&&ch[0][len-1]==' '&&
                ch[1][len-1]=='|'&&ch[2][len-1]=='|') a[n][1]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]=='_'&&ch[1][len-2]=='_'&&ch[2][len-3]=='|'&&ch[2][len-2]=='_'&&
           ch[0][len-1]==' '&&ch[1][len-1]=='|'&&ch[2][len-1]==' ') a[n][2]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]=='_'&&ch[1][len-2]=='_'&&ch[2][len-2]=='_'&&ch[0][len-1]==' '&&
                ch[1][len-1]=='|'&&ch[2][len-1]=='|') a[n][3]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]==' '&&ch[1][len-3]=='|'&&ch[1][len-2]=='_'&&ch[2][len-2]==' '&&
           ch[0][len-1]==' '&&ch[1][len-1]=='|'&&ch[2][len-1]=='|') a[n][4]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]=='_'&&ch[1][len-3]=='|'&&ch[1][len-2]=='_'&&ch[2][len-2]=='_'&&
           ch[0][len-1]==' '&&ch[1][len-1]==' '&&ch[2][len-1]=='|') a[n][5]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]=='_'&&ch[1][len-3]=='|'&&ch[1][len-2]=='_'&&ch[2][len-3]=='|'&&
           ch[2][len-2]=='_'&&ch[0][len-1]==' '&&ch[1][len-1]==' '&&ch[2][len-1]=='|') a[n][6]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]=='_'&&ch[1][len-2]==' '&&ch[2][len-2]==' '&&ch[0][len-1]==' '&&
                ch[1][len-1]=='|'&&ch[2][len-1]=='|') a[n][7]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]=='_'&&ch[1][len-3]=='|'&&ch[1][len-2]=='_'&&ch[2][len-3]=='|'&&
           ch[2][len-2]=='_'&&ch[0][len-1]==' '&&ch[1][len-1]=='|'&&ch[2][len-1]=='|') a[n][8]=1;
        if(ch[0][len-3]==' '&&ch[0][len-2]=='_'&&ch[1][len-3]=='|'&&ch[1][len-2]=='_'&&ch[2][len-2]=='_'&&
           ch[0][len-1]==' '&&ch[1][len-1]=='|'&&ch[2][len-1]=='|') a[n][9]=1;
        for(int i=2+2;i<len-2;i+=2)
        {
            if(ch[0][i-1]=='_'&&ch[1][i-1]==' '&&ch[2][i-1]=='_') a[i/2][0]=1;
            if(ch[0][i-1]==' '&&ch[1][i-1]==' '&&ch[2][i-1]==' ') a[i/2][1]=1;
            if(ch[0][i-1]=='_'&&ch[1][i-1]=='_'&&ch[2][i-1]=='_') a[i/2][2]=a[i/2][3]=a[i/2][5]=a[i/2][6]=a[i/2][8]=a[i/2][9]=1;
            if(ch[0][i-1]==' '&&ch[1][i-1]=='_'&&ch[2][i-1]==' ') a[i/2][4]=1;
            if(ch[0][i-1]=='_'&&ch[1][i-1]==' '&&ch[2][i-1]==' ') a[i/2][7]=1;
        }
        memset(dp,0,sizeof dp);
        for(int i=0;i<=9;i++) dp[1][i]=a[1][i];
        for(int i=2;i<=n;i++)
        {
            for(int j=0;j<=9;j++)
            {
                if(!a[i-1][j]) continue;
                for(int k=0;k<=9;k++)
                {
                    if(!a[i][k]) continue;
                    if(ch[0][(i-1)*2]==' '&&ch[1][(i-1)*2]==' '&&ch[2][(i-1)*2]==' ') continue;
                    if(ch[0][(i-1)*2]==' '&&ch[1][(i-1)*2]=='|'&&ch[2][(i-1)*2]==' ')
                    {
                        if(j==2&&(k==1||k==3||k==4||k==5||k==7||k==9)) dp[i][k]+=dp[i-1][j],dp[i][k]%=mod;
                    }
                    if(ch[0][(i-1)*2]==' '&&ch[1][(i-1)*2]==' '&&ch[2][(i-1)*2]=='|')
                    {
                        if((j==5||j==6)&&(k==1||k==2||k==3||k==7)) dp[i][k]+=dp[i-1][j],dp[i][k]%=mod;
                    }
                    if(ch[0][(i-1)*2]==' '&&ch[1][(i-1)*2]=='|'&&ch[2][(i-1)*2]=='|')
                    {
                        if(j==0||j==1||j==3||j==4||j==7||j==8||j==9) dp[i][k]+=dp[i-1][j],dp[i][k]%=mod;
                        if(j==2&&(k==0||k==2||k==6||k==8)) dp[i][k]+=dp[i-1][j],dp[i][k]%=mod;
                        if((j==5||j==6)&&(k==0||k==4||k==5||k==6||k==8||k==9)) dp[i][k]+=dp[i-1][j],dp[i][k]%=mod;
                    }
                }
            }
        }
        LL ans=0;
        for(int i=0;i<=9;i++) ans+=dp[n][i],ans%=mod;
        printf("%lld\n",ans);
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值