fork(1) download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string>
  4. #include <memory.h>
  5. using namespace std;
  6. typedef long long ll;
  7. class LuckySum{
  8. public:
  9. string s,res;
  10. bool dp[32][10][2][2];
  11. int v[3]={0,4,7};
  12. bool calc(int i,int h,bool s1,bool s2){
  13. if(i==s.size())
  14. return h==0 && s1 && s2;
  15. if(dp[i][h][s1][s2])
  16. return false;
  17. dp[i][h][s1][s2]=true;
  18. for(int d=!i;d<10;++d)
  19. for(int newH=0;newH<10;++newH){
  20. if(s[i]!='?' && s[i]-'0'!=d)
  21. continue;
  22. for(int a=s1;a<3;++a)
  23. for(int b=s2;b<3;++b){
  24. if((v[a]+v[b]+newH)/10!=h || (v[a]+v[b]+newH)%10!=d)
  25. continue;
  26. res+=d+'0';
  27. if(calc(i+1,newH,a>0,b>0))
  28. return true;
  29. res.resize(res.size()-1);
  30. }
  31. }
  32. return false;
  33. }
  34. long long construct(string note){
  35. s=note;
  36. memset(dp,0,sizeof(dp));
  37. res="";
  38. if(calc(0,0,0,0)){
  39. ll n;
  40. sscanf(&res[0],"%lld",&n);
  41. return n;
  42. }
  43. return -1;
  44. }
  45. };
  46. int main()
  47. {
  48. string s;
  49. cin>>s;
  50. LuckySum q;
  51. cout<<q.construct(s)<<endl;
  52. }
Success #stdin #stdout 0s 3420KB
stdin
?????
stdout
11888