DP问题专项

一. General Idea

DP ≈ recursion + memorization + guessing
memorize(remember) & reuse solutions to subproblems that help solve the problem.
time = #subproblems * time/subproblem, treating recursive call as Θ(1).

Bottom-up DP algorithm: topological sort of subproblem dependency DAG
often can save space

Guessing: try all guesses and take the best one

Shortest paths:
guess: 既然不知道哪条路到最后的节点最短那就全试一遍,然后选最短的路(无法解决环路问题)

为解决环路问题,引入了k

二. 5 “easy” steps to DP:

  1. define subproblems
    #subproblems
  2. guess
    #choices for guess
  3. relate subproblem solutions
    time/subproblem (similar to #choices for guess)
  4. recurse & memorize or build DP table bottom-up
    check subproblem recurrence is acyclic, i.e. has topological order
    total time
  5. solve original problem
    extra time

三. Subproblems for strings/sequences

  1. suffixes x[i:] ∀i
  2. prefixes x[:i] ∀i
  3. substrings x[i:j] ∀i≤j
    once find I need to use both 1 and 2, then mostly I need to use 3

四. Kinds of guessing

1 in 2(&3): guessing which subproblem to use to solve the bigger subproblem

2 in 1: add more subproblems to guess/remember more

Related post

  1. 链接原理浅析(基于Unix ELF文件格式)

    2022-01-23

  2. SQL存储函数、过程、视图、游标、触发器

    2020-08-02

  3. 网址收藏夹

    2020-07-16

  4. 异常处理的合理使用与防御式编程

    2022-02-21

There are no comment yet.

COMMENT

Take a Coffee Break

Recommend post

  1. 常用工具指令

    2022-09-18

Category list

ABOUT

Welcome to FullStar, a captivating online destination where the realms of software development and personal reflections intertwine.

May 2025
M T W T F S S
 1234
567891011
12131415161718
19202122232425
262728293031  

Life Logs

  1. 回首

    2023-07-14

Return Top