zoukankan      html  css  js  c++  java
  • CF296B Yaroslav and Two Strings(DP)

    题意翻译

    如果两个只包含数字且长度为 nn 的字符串 ss 和 ww 存在两个数字 1leq i,jleq n1i,jn,使得 s_i<w_i,s_j>w_jsi<wi,sj>wj,则称 ss 和 ww 是不可比的。现在给定两个包含数字和问号且长度为 nn 的字符串,问有多少种方案使得将所有问号替换成0到9的数字后两个字符串是不可比的?

    题解:

    /*
     *CF296B
     *线性DP
     *dp(i,k)表示遍历到第i位两个字符串的状态
     *k=0:前面没有大于或小于
     *k=1:前面只有大于
     *k=2:前面只有小于
     *k=3:前面既有小于又有大于
     */
    #include<bits/stdc++.h>
    using namespace std;
    const int maxn=2e5+100;
    const int mod=1e9+7;
    typedef long long ll;
    ll dp[maxn][4];
    int n;
    string s,t;
    int main () {
        scanf("%d",&n);
        cin>>s>>t;
        dp[0][0]=1;
        for (int i=1;i<=n;i++) {
            if (s[i-1]!='?'&&t[i-1]!='?') {
                if (s[i-1]==t[i-1]) {
                    dp[i][0]=dp[i-1][0];
                    dp[i][1]=dp[i-1][1];
                    dp[i][2]=dp[i-1][2];
                    dp[i][3]=dp[i-1][3];
                }
                else if (s[i-1]<t[i-1]) {
                    dp[i][0]=0;
                    dp[i][1]=0;
                    dp[i][2]=dp[i-1][2]+dp[i-1][0];dp[i][2]%=mod;
                    dp[i][3]=dp[i-1][3]+dp[i-1][1];dp[i][3]%=mod;
                }
                else if (s[i-1]>t[i-1]) {
                    dp[i][0]=0;
                    dp[i][1]=dp[i-1][0]+dp[i-1][1];dp[i][1]%=mod;
                    dp[i][2]=0;
                    dp[i][3]=dp[i-1][2]+dp[i-1][3];dp[i][3]%=mod;
                }
            }
            else if (s[i-1]=='?'&&t[i-1]!='?') {
                int t1='9'-t[i-1];
                int t2=t[i-1]-'0';
                dp[i][0]=dp[i-1][0];
                dp[i][1]=dp[i-1][1]*(t1+1)%mod+dp[i-1][0]*t1%mod;dp[i][1]%=mod;
                dp[i][2]=dp[i-1][2]*(t2+1)%mod+dp[i-1][0]*t2%mod;dp[i][2]%=mod;
                dp[i][3]=dp[i-1][1]*t2%mod+dp[i-1][2]*t1%mod+dp[i-1][3]*10%mod;dp[i][3]%=mod;
            }
            else if (s[i-1]!='?'&&t[i-1]=='?') {
                int t2='9'-s[i-1];
                int t1=s[i-1]-'0';
                dp[i][0]=dp[i-1][0];
                dp[i][1]=dp[i-1][1]*(t1+1)%mod+dp[i-1][0]*t1%mod;dp[i][1]%=mod;
                dp[i][2]=dp[i-1][2]*(t2+1)%mod+dp[i-1][0]*t2%mod;dp[i][2]%=mod;
                dp[i][3]=dp[i-1][1]*t2%mod+dp[i-1][2]*t1%mod+dp[i-1][3]*10%mod;dp[i][3]%=mod;
            }
            else {
                int t1=45;
                int t2=45;
                int t3=10;
                dp[i][0]=dp[i-1][0]*t3;
                dp[i][1]=dp[i-1][1]*(t1+t3)%mod+dp[i-1][0]*t1%mod;dp[i][1]%=mod;
                dp[i][2]=dp[i-1][2]*(t2+t3)%mod+dp[i-1][0]*t2%mod;dp[i][2]%=mod;
                dp[i][3]=dp[i-1][1]*t2%mod+dp[i-1][2]*t1%mod+dp[i-1][3]*(t1+t2+t3)%mod;dp[i][3]%=mod;
            }
        }
        printf("%lld
    ",dp[n][3]);
    } 
  • 相关阅读:
    Knol of Fabio Maulo
    调用非.net系统的Webservice的探索 ( 二 ) WSE
    在Sql Server 使用系统存储过程sp_rename修改表名或列名
    Who is locking the DB account?
    (python learn) 7 字典
    (python learn) 8 流程控制
    (python learn) 4 number&& string
    where is the data come from after we drop the table
    (healthy recorder) 治疗第6天
    (python learn) 6 列表
  • 原文地址:https://www.cnblogs.com/zhanglichen/p/13479361.html
Copyright © 2011-2022 走看看