给定一个带通配符问号的数W,问号可以代表任意一个一位数字。
再给定一个整数X,和W具有同样的长度。
问有多少个整数符合W的形式并且比X大?
输入格式
多组数据,每组数据两行,第一行是W,第二行是X,它们长度相同。在[1..10]之间.
输出格式
每行一个整数表示结果。
答题说明:
输入样例
36?1?8
236428
8?3
910
?
5
输出样例
100
0
4
#include <stdio.h>
#include "stdlib.h" // _itoa()
#include <iostream>
#include <string.h>
#include <sstream>
//#include <hash_map>
#include <ext/hash_map>
#include <cstdlib>
using namespace std;
using namespace __gnu_cxx;
struct str_hash
{
size_t operator()(const string& str) const
{
return __stl_hash_string(str.c_str());
}
};
int getCount2(char*a,char*b,int mark,int deep)
{
string tname = "";
tname += '0'+deep;
tname += '_';
tname += '0'+mark;
//cout << tname<<endl;
static hash_map<string, int, str_hash> myhash;
if(deep == 0)
{
myhash.clear();
}
if(myhash.find(tname) != myhash.end())
{
return myhash[tname];
}
if(!*a)
{
return mark;
}
if(mark==0)
{
if(*a=='?')
{
//返回一个相等和多个不等
int r=getCount2(a+1,b+1,0,deep+1) + ('9'-*b)*getCount2(a+1,b+1,1,deep+1);
myhash[tname]=r;
return r;//('9'==*b)?0:
}
else if(*a>*b)
{
int r=getCount2(a+1,b+1,1,deep+1);
myhash[tname]=r;
return r;
}
else if(*a==*b)
{
int r=getCount2(a+1,b+1,0,deep+1);
myhash[tname]=r;
return r;
}
else if(*a<*b)
{
return 0;
}
}
else if(mark==1)
{
if(*a=='?')
{
//返回一个相等和多个不等
int r=10*getCount2(a+1,b+1,1,deep+1);
myhash[tname]=r;
return r;
}
else
{
int r=1*getCount2(a+1,b+1,1,deep+1);
myhash[tname]=r;
return r;
}
}
}
int main()
{
while(1)
{
char str1[1024];
char str2[1024];
scanf("%s", str1);
if(*str1 =='q'||*str1=='\n') break;
scanf("%s", str2);
printf("%d",getCount2(str1,str2,0,0));
}
return 0;
}