【问题标题】:Cannot understand the logic of editorial看不懂编辑逻辑
【发布时间】:2018-01-30 13:20:34
【问题描述】:

问题链接:https://www.codechef.com/problems/STR

问题是:

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. 

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

我在这里无法理解 dp 的逻辑。有人可以帮助我吗?

【问题讨论】:

  • 欢迎来到 StackOverflow。请阅读并遵循帮助文档中的发布指南。 on topichow to ask 在这里申请。 StackOverflow 不是设计、编码、研究或教程服务。
  • "Can Someone Help Me?" is not a valid SO question。这通常表明您需要的是与当地导师一起半小时或完成教程,而不是 Stack Overflow。

标签: algorithm dynamic-programming bitmask


【解决方案1】:

为简单起见,假设我们使用了从“a”到“f”的前 6 个字符,而不是 20 个字符。 我们将每个字符串存储在 6 位中,将它们包含的字符加上 1(例如,“abc”的位掩码可以是 111000)。

字符串 s 的 supermask 满足以下条件:

  • 如果 s 的第 i 位为 1,则超级掩码的第 i 位为 1。
  • 如果 s 的第 i 位为 0,则超级掩码的第 i 位可以是 0 或 1。

s = 111000 的超级掩码是111000, 111001 ... 111111我们将 x 表示为最大可能 s 值的整数表示,即 63。注意对于字符串 s:

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

作者建议的第一个解决方案是:假设您已经计算了数字i+1, i+2 ... x 的所有超级掩码的计数,其中0 <= i <= x让 bit(i, k) 表示输入位掩码 i 的第 k 个最低有效位 (for i = "111000", bit(i, 2) = 0)。最后,让 dp[i] 表示 i 的超级掩码数。该算法表明,

  • 元素是自身的超级掩码 (dp[i] = 1)
  • 从最低有效位到最高有效位,每当您在索引 k 上遇到 0 时
    • 如果将位 k 翻转为 1,则结果 i' 是 i (dp[i]++) 的超级掩码
    • i' 的所有超级掩码都是 i (dp[i] += dp[i | bit(i, k)]) 的超级掩码

问题是这个解决方案多次计算相同的超级掩码。 考虑i = 111000 的情况,它对i' = 111001i'' = 111010 都计算超级掩码111111。您需要找到一种方法来消除这些重复项。

作者建议的最后一点如下:dp[i][j]表示i的超级掩码数,使得i的最右边j个0位全为零。例如@987654338 @、dp[i][j] 包括 111000111100。使用这种方法,迭代 i = 111000 给出:

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

不幸的是,作者的文档非常糟糕,我无法理解他最终提出问题时使用的符号。不过,我希望解释对理解逻辑很有用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多