-1

I have a dynamic programming exercise but I dont know how it will work in here:

We have a mysterious number is a string that consists of digits and asterisk *. Given a mysterious number, count all the possible natural number to replace the asterisk * with a digit to produce an integer divisible by n. For expample:

For 1*1* and n = 6 there are 16 possible numbers divisible by n: 1014, 1110, 1116, 1212, 1218, 1314, 1410, 1416, 1512, 1518, 1614, 1710, 1716, 1812, 1818, 1914.

If there is a leading asterisk then it should never be replaced by a zero:

*12 -> 112 (is okay) but *12 -> 012 (is not okay)

Input:

1 <= inputString.size <= 1000

1 <= n <= 1000

Time limit :

500ms in C++ language. As i said this is an dynamic programming exercise.

Can anyone give me some hints on this?

Justin
  • 149
  • 1
  • 11
  • Do you know the value of `n` in advance or is that part of the input of your program? (If known in advance there may be shortcuts: for `n=6` the last digit must be even and the digits must sum to a multiple of 3.) If `n` is not known in advance, is there something wrong with brute-force (replace the asterisks with digits and test all possibilities with division by `n`)? Is there a limit to the size of the string or to the size of the integers in your programming language? These details are needed for a good solution. – Rory Daulton Nov 29 '17 at 10:52
  • Sorry for my mistake, n is not known in advance: n is in range 1-1000 and the size of misterious number is in range of 1 -1000 with time limit is 500ms with C++ language. So i dont think there will be a place for brute-force @RoryDaulton – Justin Nov 29 '17 at 11:09
  • if you need i can give u code for Recursive alghoritm. but no idea for dynamic. also 500 ms is a lot of time!!!!! this alghoritm just need 1ms – Nozar Safari Nov 29 '17 at 11:23
  • I agree: 500 milliseconds is a lot of time. Given your limits there will be at most 3 stars which can easily be done by brute force. – Rory Daulton Nov 29 '17 at 11:35

1 Answers1

1

i write a JavaScript for you it work, you can change it to c++, also leave comment for u. it use Recursive to handle and replace every * from left and replace it with possible numbers

var string = "sample mystriuos number"

function Func(mysNumber , n ) 
{
    // this function 
    var realnumber = true // a check if number is not mystrius anymore!!
    for (var i=0 ; i<= mysNumber.length ; i++)
    {
        if(i !=0 && mysNumber[i]=="*") // for  Not left number
        {
            realnumber= false
            for(var j=0 ; j<=9 ;j++ )
            {
                Func(mysNumber.substring(0,i) + j.toString() + mysNumber.substring(i+1,mysNumber.length) ,n)
            }
        }
        else if (mysNumber[i]=="*") // for left number 
        {
            realnumber = false
            for(var j=1 ; j<=9 ;j++ )
            {
                Func(mysNumber.substring(0,i) + j.toString() + mysNumber.substring(i+1,mysNumber.length) ,n)
            }

        }
    }

    if(realnumber)
        if(parseInt(mysNumber) % n ==0)
            print(mysNumber)


}
func(string , 6)
Nozar Safari
  • 505
  • 4
  • 17