-1

Question link : https://www.codechef.com/problems/STR

question is :

Little John just had his first class in school. He was taught first 20 
letters of English alphabet and was asked to make words from these 
alphabets. 

Since he doesn't know many dictionary words, he quickly finished this work 
by making random strings from these alphabets.
Now while other kids are busy creating their words, John gets curious and 
puts all the strings he created in a list and named it X.



He picks two indices 'i' and 'j' ( not necessarily distinct). He assigns A 
as X[i] and B as X[j]. He then concatenates both the strings to create a new 
string C ( = A + B ). He calls a string "super string" if that string 
contains all the 20 letters of English alphabet he has just learnt,atleast 
once.



Given the strings of the list, can you tell him how many such unordered 
pairs (i,j) he can choose such that string C is a super string. 

Editorial : https://discuss.codechef.com/questions/79843/str-editorial

I cannot understand logic of dp here.Can someone help me ?

Varun Kamani
  • 86
  • 1
  • 8
  • Welcome to StackOverflow. Please read and follow the posting guidelines in the help documentation. [on topic](http://stackoverflow.com/help/on-topic) and [how to ask](http://stackoverflow.com/help/how-to-ask) apply here. StackOverflow is not a design, coding, research, or tutorial service. – Prune Jan 30 '18 at 16:24
  • ["Can Someone Help Me?" is not a valid SO question](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question). This usually suggests that what you need is half an hour with a local tutor or walk through a tutorial, rather than Stack Overflow. – Prune Jan 30 '18 at 16:24

1 Answers1

0

For the sake of simplicity, assume that we used first 6 characters from 'a' to 'f' instead of 20 characters. We will store each string in 6 bits by putting 1s for the characters they contain (for example, the bitmask of "abc" can be 111000).

A supermask of a string s satisfies the following:

  • If i-th bit of s is 1, i-th bit of the supermask is 1.
  • If i-th bit of s is 0, i-th bit of the supermask can be either 0 or 1.

Supermasks of s = 111000 are 111000, 111001 ... 111111. Let's denote x as the integer representation of maximum possible s value, which 63. Notice that for a string s:

s | x - s = x (111000 | 000111 = 56 + 7)

The first solution that author suggests is this: Assume that you have calculated the count of all supermasks for numbers i+1, i+2 ... x where 0 <= i <= x. Let bit(i, k) denote k-th least significant bit of input bitmask i (for i = "111000", bit(i, 2) = 0). Finally, let dp[i] denote the count of supermasks of i. The algorithm suggests that,

  • An element is a supermask of itself (dp[i] = 1)
  • From least significant bit to most significant, whenever you encounter a 0 on index k
    • If you flip bit k to 1, result i' is a supermask of i (dp[i]++)
    • All supermasks of i' are supermasks of i (dp[i] += dp[i | bit(i, k)])

The problem is this solution counts the same supermasks multiple times. Consider the case when i = 111000, it counts supermask 111111 for both i' = 111001 and i'' = 111010. You need to find a way to eliminate these duplicates.

The final thing that author suggests is as follows: Let dp[i][j] denote the number of supermasks of i, such that rightmost j 0-bits of i are all zeros. For example for i = 111000, dp[i][j] includes 111000 and 111100. Using this approach, iterating i = 111000 gives:

  • dp[i][0] = 111001, 111011, 111101, 111111
  • dp[i][1] = 111010, 111110
  • dp[i][2] = 111100

Unfortunately, the documentation of the author was very bad and I wasn't able to understand the notation used in his final formulation of the problem. Still, I hope that the explanation is useful enough to understand the logic.

Can Bayar
  • 497
  • 4
  • 16