TopCoder algorithm problem - how to make progress on this?

I recently joined TopCoder and practiced in Practice Rooms for the past few days. I ran into this problem which I seem to be unable to solve. Any help would be appreciated.

Problem

The product value for a string is the product of all the digits ('0' - '9') in String. For example, a product value of "123" is 1 * 2 * 3 = 6. A line is called colored if it contains only numbers and the product value of each of its non-empty adjacent substrings is different.

For example, the string "263" has six substrings: "2", "6", "3", "26", "63", and "263". The product values ​​of these Substrings are: 2, 6, 3, 2 * 6 = 12, 6 * 3 = 18, and 2 * 6 * 3 = 36, respectively. Because all six value products are different, "263" is colorful.

On the other hand, "236" is not colorful, because its two substrings "6" and "23" have the same product value (6 = 2 * 3).

Returns the kth (1-based) lexicographically smallest colorful string of length n. If there are less than k colored strings of length n, return an empty string instead.

My approach

"0" "1" n. . , n 9. ( 2, 3, 4, 5, 6, 7, 8, 9, ).

, "23" ( 2- ) (, - , ) n.

n, "" , k- .

, . , , k-- ?

? ?

, . - .

, , , . , , , - , , - .

=)

* , SRM 464 DIV 2 - 500pt. . TopCoder.

+3
4

Topcoder , SRM (464 ). , :)

+4

, .

? "": 8 , . 8 * 7 + 8 * 7 * 6 + 8 * 7 * 6 * 5 +... 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1 = 109592, .

, , "" .

+2

- : n , , n-1, . , , .

, colorful(n), n. , :

colorful(n):
  if n = 1:
    return { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9" }

  def colorful_subs = colorful(n-1)
  def colorfuls

  for each sub in colorful_subs:
    remaining_digits = { 2, 3, 4, 5, 6, 7, 8, 9 } - digits_in(sub)

    for each digit in remaining_digits:
      if is_colorful(sub, digit):
        colorfuls += (sub + digit)

  return colorfuls

is_colorful , , , .

colorful(n) k - . ( , "0" "1", n= 1)

This is mainly dynamic programming . I am sure that this can be improved - there can be a reasonable way to find out if a certain digit will add to the colorful number so that the number is no longer bright without completing it and checking. But this, of course, checks a much smaller number than all possible permutations 2-9.

+2
source

Here is the quick code that I put together seemed like a funny question ... I hope I did not understand the problem.

Greetings

    public class Colorful {


    /**
     * Find the k-th colorful number that has n digits
     * @param n The number of digits
     * @param k The k-th colorful position
     * @return  The colorful number 
     */
    public static String findColorfulNumber(int n, int k) {
        int start=(int) Math.pow(10, n-1);
        int end=(int) Math.pow(10, n);
        Stack<String> colorfulNumbers=new Stack<String>();
        for(int i=start;i<end;i++){
            String currentNumber=i+"";
            if(isColorful(currentNumber)){
                colorfulNumbers.push(currentNumber);
                if(colorfulNumbers.size()==k)
                    break;
            } 
        }

        return colorfulNumbers.size()==k?"":colorfulNumbers.pop();
    }

    /**
     * Checks if a given number is colorful
     * @param number    The number to check
     * @return  Returns <code>true</code> if the number is colorful, <code>false</code> otherwise
     */
    private static boolean isColorful(String number) {
        char[] numberAsArray = number.toCharArray();
        Set<Integer> uniqueProducts=new LinkedHashSet<Integer>();

        return generateUniqueNumbers(numberAsArray,1,uniqueProducts);
    }


    /**
     * Recursive function that checks whether a number is colorful or not
     * @param numberAsArray The number as an array
     * @param charCount The sequence size of a number of characters to check at a given recursive iteration, starts from 0 ends at array length... 
     * @param uniqueProducts    The set of unique products computed until current iteration 
     * @return
     */
    private static boolean generateUniqueNumbers(char[] numberAsArray, int charCount,
            Set<Integer> uniqueProducts) {

        if(charCount>numberAsArray.length)
            return true;

        for(int i=0;(i+charCount-1)<numberAsArray.length;i++){
            int product=1;
            for(int j=0;j<charCount;j++){
                product=product*Integer.parseInt(new String(numberAsArray[i+j]+""));
            }
            if(!uniqueProducts.add(product))
                return false;
        }
        return generateUniqueNumbers(numberAsArray,charCount+1,uniqueProducts);
    }
}
0
source

Source: https://habr.com/ru/post/1755392/


All Articles