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 I N X is equal to the character in the same position in W (it means so can replace a question mark W ith any digit). For example, 365198 matches the wild number 36?1?8, but 360199, 361028, or 36128 does not. Write A program This reads a wild number W and a number X from input, both of length n, and Det Ermines the number of n-digit numbers that match W and is greater than x.
Input
There is multiple test cases in the input. Each test case consists of 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 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?82364288?3910?5#
Sample Output
10004
Source
Tehran 2006
/*
Back to the first question, a few days without the brain rusty
*/
#include <iostream>#include<cstring>#include<cstdio>#include<cmath>#defineN 20using namespacestd;Long LongPowerLong LongALong Longb//a function similar to the POW function, because it's rather too small.{ Long Longans=1; for(intI=1; i<=b;i++) ans*=Ten; returnans;}intMain () {//freopen ("In.txt", "R", stdin); CharA[n],b[n]; while(SCANF ("%s", &a)!=eof&&strcmp (A,"#")!=0) { Long Longans=0;//Timer intsum=0, cur=0;//Count The number of question marks and the number of times the question mark has been enumerated so farscanf"%s",&b); for(intI=0; a[i];i++) if(a[i]=='?') Sum++; for(intI=0; a[i];i++) { if(a[i]!='?') { if(a[i]>B[i]) {ans+=power (Ten, sum-cur); Break; } Else if(a[i]<B[i]) Break; } Else if(a[i]=='?') {cur++; Ans+=power (Ten, Sum-cur) * (9-b[i]+'0'); }} printf ("%lld\n", ans); } return 0;}
POJ 3340 Barbara Bennett ' s Wild Numbers (digital DP)