Пожалуйста, прочтите новое правило об ограничении использования AI-инструментов. ×

Блог пользователя gagannagpal68

Автор gagannagpal68, история, 3 года назад, По-английски

Given a string s and a number k. You need to tell count of distinct string of length k. s.t. k is a subset of s.

Eg 1. "aaabb" 2 => 4 (aa bb ab ba)

  1. "aabbcc" 2 => 9( aa ab ac ba bb bc ca cb cc)

  2. "abbcc" 2 => 8( same as #2 but aa can't be formed

I can create a dp of state index and mask where mask is of base 26 and rest is straightforward. Please help me in finding some combinatorics approach of solving it.

  • Проголосовать: нравится
  • +24
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by gagannagpal68 (previous revision, new revision, compare).

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

I have something want to say.

I have another dp state that dp[26][len] means the distinct string of length len where we have used which alpha. Transition is simple dp[i+1][len+c] <- dp[i][len] * C(len+c, c)

And I have another problem related with your problem. Here it is . The difference is that we must use all the char in the original string. Hope you enjoy solve this problem.

»
3 года назад, # |
Rev. 2   Проголосовать: нравится +32 Проголосовать: не нравится

i guess its classic application of E.G.F.
lets set of characters be $$$a_i,f_i $$$, here $$$f_i$$$ represents the frequency
your answer will be
$$$ [x^k] \prod_{i} (\sum_{j=0}^{f_i}\frac{x^j}{j!})$$$
here $$$\sum_{j=0}^{f_i}\frac{x^j}{j!} $$$ is generating function for character $$$a_i$$$ with frequenccy $$$f_i$$$

multiply by k! as we are using coeffiecient of $$$x^k$$$

like for first example answer is $$$2! \times [ \frac{1}{2!} + \frac{1}{1!} + \frac{1}{2!} ]$$$
which is equal to 4.

»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

I have a dp + combinatorics solution

let dp[i] denote the number of strings of length i, we have parsed till the jth alphabet. Lets say we take l number of jth alphabet

dp[i]+=dp[i-l]*nCr(i,l);
  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится 0 Проголосовать: не нравится

    J is not used anywhere — are we considering sum(nCr for l = 1..J))? k is not used anywhere — answer is the same for every k?

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      No means actually it is dp[i][j] but you can compress the 2D DP into only dp[i] in knapsack right ? Also final answer is dp[k].

      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится 0 Проголосовать: не нравится
        Still no clue
        • »
          »
          »
          »
          »
          3 года назад, # ^ |
            Проголосовать: нравится +3 Проголосовать: не нравится

          Not sure what you are not getting, maybe this code can help.

          Code
          • »
            »
            »
            »
            »
            »
            3 года назад, # ^ |
            Rev. 3   Проголосовать: нравится 0 Проголосовать: не нравится

            Yep, thanks! This makes it all clear. For every d[i] you count how many times you can add some letter L = 1..freq(letter) times, using previously calculated dp[i-L] for previous letters. This looks like O(k*length(s)) solution.