poj 3340 Barbara Bennett's Wild Numbers(数位DP)

Barbara Bennett's Wild Numbers
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 3153   Accepted: 1143

Description

A wild number is a string containing digits and question marks (like 36?1?8). A number X matches a wild number W if they have the same length, and every non-question mark character in X is equal to the character in the same position in W (it means that you can replace a question mark with any digit). For example, 365198 matches the wild number 36?1?8, but 360199, 361028, or 36128 does not. Write a program that reads a wild number W and a number X from input, both of length n, and determines the number of n-digit numbers that match W and are greater than X.

Input

There are multiple test cases in the input. Each test case consists of two lines of the same length. The first line contains a wild number W, and the second line contains an integer number X. The length of input lines is between 1 and 10 characters. The last line of input contains a single character #.

Output

For each test case, write a single line containing the number of n-digit numbers matching W and greater than X (n is the length of W and X).

Sample Input

36?1?8
236428
8?3
910
?
5
#

Sample Output

100
0
4

Source

/*
回来的第一道题,几天不用脑子生锈了
*/
#include<iostream> #include<cstring> #include<cstdio> #include<cmath> #define N 20 using namespace std; long long power(long long a,long long b)//类似于pow函数的一个功能,因为颇为太小了 { long long ans=1; for(int i=1;i<=b;i++) ans*=10; return ans; } int main() { //freopen("in.txt","r",stdin); char a[N],b[N]; while(scanf("%s",&a)!=EOF&&strcmp(a,"#")!=0) { long long ans=0;//计时器 int sum=0,cur=0;//计算问号的次数和枚举目前为止出现问号的次数 scanf("%s",&b); for(int i=0;a[i];i++) if(a[i]=='?') sum++; for(int i=0;a[i];i++) { if(a[i]!='?') { if(a[i]>b[i]) { ans+=power(10,sum-cur); break; } else if(a[i]<b[i]) break; } else if(a[i]=='?') { cur++; ans+=power(10,sum-cur)*(9-b[i]+'0'); } } printf("%lld ",ans); } return 0; }
原文地址:https://www.cnblogs.com/wuwangchuxin0924/p/5780033.html