V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
lcj2class
V2EX  ›  程序员

请教一道算法题

  •  
  •   lcj2class · 2019-01-21 17:10:02 +08:00 · 1917 次点击
    这是一个创建于 1912 天前的主题,其中的信息可能已经有所发展或是发生改变。

    Given an array with positive integers and another integer for example {7 2 4} and 9, you are required to generate an equation, by inserting operator add ("+") and minus ("-") among the array. The left side of equation are consist of the array and the right side of equation is the integer. Here the result is 7-2+4=9.

    无意间在 http://www.raychase.net/1285 里看到的,作者没去说这题的思路。除了穷举外,还有什么好方法呢 🤔

    9 条回复    2019-01-22 17:18:45 +08:00
    geelaw
        1
    geelaw  
       2019-01-21 17:16:44 +08:00 via iPhone
    这个问题(很直白地)是 subset sum,数字都很小的时候用最常见的那个 dynamic programming。此外还可以用 @ChaoXu 之前的研究结果。

    该问题是 NP-hard,所以不要期待一个多项式时间的算法。
    enenaaa
        2
    enenaaa  
       2019-01-21 17:27:24 +08:00
    @geelaw 请教下为啥数字大的时候不能用动态规划
    guyeu
        3
    guyeu  
       2019-01-21 17:29:14 +08:00
    emmmm 这个问题比 subset sum 多了个限定条件,感觉用二叉树+剪枝可以试试。
    geelaw
        4
    geelaw  
       2019-01-21 17:44:12 +08:00 via iPhone
    @enenaaa #2 数字大的时候动态规划不如枚举有效率
    qinyusen
        5
    qinyusen  
       2019-01-21 18:21:25 +08:00
    qinyusen
        6
    qinyusen  
       2019-01-21 18:22:38 +08:00
    @geelaw 额,刚才 at 错人了。。。
    aijam
        7
    aijam  
       2019-01-22 05:06:29 +08:00
    lcj2class
        8
    lcj2class  
    OP
       2019-01-22 14:34:32 +08:00
    @geelaw #4 是说数字大的时候会有额外的 overhead,有没有相关文章介绍的呢?
    geelaw
        9
    geelaw  
       2019-01-22 17:18:45 +08:00 via iPhone
    @lcj2class 举个例子,有 n 个数,最大的数大小是 3^n,则 DP 的时间复杂度是 Omega(3^n) 但枚举的时间复杂度是 O(2^n*poly(n))。

    如果你用“刷新式”实现动态规划则另谈。
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2794 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 15:29 · PVG 23:29 · LAX 08:29 · JFK 11:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.