【问题标题】:How can dynamic programming be applied here?如何在这里应用动态规划?
【发布时间】:2012-10-21 13:43:12
【问题描述】:

我正在解决这个问题http://www.spoj.pl/problems/DISUBSTR/ 给定一个字符串,我们需要找到它的不同子字符串的总数。

Input

T- number of test cases. T<=20;
Each test case consists of one string, whose length is <= 1000

Output

For each test case output one number saying the number of distinct substrings.

Example

Sample Input:
2
CCCCC
ABABA

Sample Output:
5
9

    Explanation for the testcase with string ABABA: 
    len=1 : A,B
    len=2 : AB,BA
    len=3 : ABA,BAB
    len=4 : ABAB,BABA
    len=5 : ABABA
    Thus, total number of distinct substrings is 9.

我已经使用盲实现后缀数组解决了这个问题。但是,我想使用动态编程来解决它。但是我想不出任何方法来达到这个目的。时间复杂度需要为 0(n*n) 或更快。请任何人都可以指导我正确的方向.任何提示/建议/伪代码将不胜感激。我已经考虑了很长时间,但想不出任何DP方法来解决问题?

【问题讨论】:

  • @DigitalDa:这是一个不同的问题
  • @DanielFisher:我不明白
  • 为什么要应用动态规划?
  • @alestanis:这几天我在练习DP问题。

标签: c++ algorithm logic dynamic-programming


【解决方案1】:

我不认为你可以使用动态规划来解决这个问题,因为没有最优的子结构。知道字符串的一部分的答案并不能告诉您有关该部分的任何信息+例如另一个字符。

这个问题可以通过在trie中插入字符串的所有后缀然后计算它的节点数来解决。这是O(n^2),很可能会用这么短的字符串得到AC。更有效的方法包括使用后缀数组 (O(n log n)) 并在 O(n) 中构建后缀树。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    相关资源
    最近更新 更多