题意

如果两个数字除了带问号的位以外都相同,我们称这两个数可以相互匹配

给你两个数,其中第一个数字里有一些问号,问有多少个大于第二个数的数字可以和第一个数字匹配

一开始懒得读题,到网上搜题意,结果居然没搜到这个题,于是决定贴一下代码

从高位到低位for循环搞一搞就好了,当然也可以dfs

#include<string>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cmath>
#include<vector>
#include<map>
#include<queue>
using namespace std;
typedef long long ll;
const int MAX=1<<29;
int n,m,len;
char str[15];
char tmp[15];
ll digit[12];
ll nums[15],ans;
void dfs(int pos,bool flag)
{
    int i,j;
    if(pos==len)
    {
        if(!flag)ans++;
        return ;
    }
    if(str[pos]=='?')
    {
        if(!flag)ans+=digit[nums[pos]];
        else
        {
            ans+=digit[nums[pos]-1]*('9'-tmp[pos]);
            dfs(pos+1,flag);
        }
    }
    else
    {
        if(flag)
        {
            if(str[pos]<tmp[pos])return ;
            else if(str[pos]==tmp[pos])dfs(pos+1,flag);
            else dfs(pos+1,0);
        }
        else dfs(pos+1,0);
    }
}
int main()
{
    int i,j;
    digit[0]=1;
    for(i=1;i<12;i++)digit[i]=digit[i-1]*10;
    while(scanf("%s",str)!=EOF)
    {
        if(str[0]=='#')return 0;
        scanf("%s",tmp);
        memset(nums,-1,sizeof(nums));
        len=strlen(str);
        nums[len]=0;
        for(i=len-1;i>=0;i--)
        {
            nums[i]=nums[i+1];
            if(str[i]=='?')nums[i]++;
        }
        ans=0;
        dfs(0,1);
        printf("%lld\n",ans);
    }

}

 

posted on 2017-07-10 12:54  曙光小学_s10  阅读(180)  评论(0编辑  收藏  举报