Round#179 diy2第二题

   题意:给你两个数字串,其中可以包含'?',求将两个数字串变成不可比较的数字串有多少种方案。(不可比较当且仅当存在i,j,使得s[i]<w[i]&&s[j]>w[j])。

题意理解清楚后,就知道这题其实就是一道排列组合的题目了。

//排列组合
#include<iostream>
#include<cstdio>
#include<cstring>

using namespace std;
const int N=100005;
const int M=1000000007;
char s[N],w[N];//输入字符串
int n;

int main()
{
    int cnt;//'?'个数
    int small,big;
    while(~scanf("%d",&n))
    {
        cnt=0;
        small=big=0;
        scanf("%s%s",s,w);
        for(int i=0;i<n;i++)
        {
            if(s[i]=='?'||w[i]=='?')
            {
                if(s[i]=='?')
                    cnt++;
                if(w[i]=='?')
                    cnt++;
            }
            else
            {
                if(s[i]<w[i])
                    small=1;//s[i]<w[i]
                else if(s[i]>w[i])
                    big=1;//s[i]>w[i]
            }
        }
        if(small==1&&big==1)//除'?'以外的其它字符已经符合题意,则'?'可以随意替换
        {
            long long ans=1;
            for(int i=1;i<=cnt;i++)
                ans=ans*10%M;
            printf("%d\n",ans);
        }
        else if(cnt==0)//无'?'
            printf("0\n");
        else if(cnt==n*2)//全'?'
        {
            long long sum,tmp1,tmp2;
            sum=tmp1=tmp2=1;
            for(int i=1;i<=cnt;i++)
                sum=sum*10%M;
            for(int i=1;i<=n;i++)
            {
                tmp1=tmp1*55%M;//大于或等于
                tmp2=tmp2*10%M;//等于
            }
            sum=(sum-tmp1*2+tmp2+M*2)%M;
            printf("%d\n",sum);
        }
        else
        {
            if(small)//小于或等于
            {
                long long sum=1,tmp=1;
                for(int i=1;i<=cnt;i++)
                    sum=sum*10%M;
                for(int i=0;i<n;i++)
                {
                    if(s[i]=='?'&&w[i]=='?')
                        tmp=tmp*55%M;
                    else if(s[i]=='?')
                        tmp=tmp*(w[i]-'0'+1)%M;
                    else if(w[i]=='?')
                        tmp=tmp*(10-(s[i]-'0'))%M;
                }
                sum=(sum-tmp+M)%M;
                printf("%d\n",sum);
            }
            else if(big)//大于或等于
            {
                long long sum=1,tmp=1;
                for(int i=1;i<=cnt;i++)
                    sum=sum*10%M;
                for(int i=0;i<n;i++)
                {
                    if(s[i]=='?'&&w[i]=='?')
                        tmp=tmp*55%M;
                    else if(s[i]=='?')
                        tmp=tmp*(10-(w[i]-'0'))%M;
                    else if(w[i]=='?')
                        tmp=tmp*(s[i]-'0'+1)%M;
                }
                sum=(sum-tmp+M)%M;
                printf("%d\n",sum);
            }
            else
            {
                long long sum,tmp1,tmp2,tmp3;
                sum=tmp1=tmp2=tmp3=1;
                for(int i=1;i<=cnt;i++)
                    sum=sum*10%M;
                for(int i=0;i<n;i++)
                {
                    if(s[i]=='?'&&w[i]=='?')
                    {
                        tmp1=tmp1*55%M;
                        tmp2=tmp2*55%M;
                        tmp3=tmp3*10%M;
                    }
                    else if(s[i]=='?')
                    {
                        tmp1=tmp1*(10-(w[i]-'0'))%M;
                        tmp2=tmp2*(w[i]-'0'+1)%M;
                    }
                    else if(w[i]=='?')
                    {
                        tmp1=tmp1*(s[i]-'0'+1)%M;
                        tmp2=tmp2*(10-(s[i]-'0'))%M;
                    }
                }
                sum=(sum-tmp1-tmp2+tmp3+M*2)%M;
                printf("%d\n",sum);
            }
        }
    }
    return 0;
}


 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值