All Tracks Algorithms Dynamic Programming Problem

String Partitioning
Tag(s):

Algorithms, Dynamic Programming, Medium-Hard

Problem
Editorial
Analytics

Given a large string of integers S of length N, divide the string into K non-empty substrings such that the sum of cost of each substring is minimised. The cost of a substring is $$\prod_{i = 0}^{9} (freq[i] + 1)$$, where $$freq[i]$$ denotes the number of occurrence of integer $$i$$ in the substring under consideration.

Input
First line contains a single integer T denoting the number of test cases.
First line of each test case contains two integers N and K.
Second line of each test case contains the string S

Output
For each test case output the minimum cost.

Constraints
1 <= T <= 50
1 <= N <= 500

1 <= K <= N

0 <= S[i] <= 9

SAMPLE INPUT
2
3 1
344
1 1
3
SAMPLE OUTPUT
6
2
Explanation

First test case:
$$freq[3] = 1$$ and $$freq[4] = 2$$. Since K=1, the minimum cost will be $$(freq[3]+1) * (freq[4]+1) = 2 * 3 = 6$$.

Time Limit: 3.0 sec(s) for each input file.
Memory Limit: 256 MB
Source Limit: 1024 KB
Marking Scheme: Marks are awarded when all the testcases pass.
Allowed Languages: C, C++, C++14, Clojure, C#, D, Erlang, F#, Go, Groovy, Haskell, Java, Java 8, JavaScript(Rhino), JavaScript(Node.js), Julia, Kotlin, Lisp, Lisp (SBCL), Lua, Objective-C, OCaml, Octave, Pascal, Perl, PHP, Python, Python 3, R(RScript), Racket, Ruby, Rust, Scala, Swift, Visual Basic

CODE EDITOR

Initializing Code Editor...
Your Rating:

Contributor

This Problem was Asked in

HackerEarth

Challenge Name

HackerEarth Collegiate Cup - Mirror Round

OTHER PROBLEMS OF THIS CHALLENGE
Notifications
View All Notifications