Overview
When optimizing dp, one can not only start with the transition process and accelerate transitions. Sometimes one can also start with the state definition and optimize complexity by changing how the state is designed.
What is rather troublesome is that most of these optimizations are not general-purpose; that is, they cannot be applied to multiple problems in a routine way. Therefore, the following sections start from concrete example problems and strive to provide inspiration at the level of ideas, hoping to be of some help to readers.
Example 1
Problem Statement
Given two strings of lengths respectively, consisting only of lowercase letters, , find the longest common subsequence of .
Naive Solution
You solved it instantly at a glance—isn't this a standard problem?
Define the state as the longest common subsequence of 's first characters and 's first characters. Then:
The time complexity of the above method is , so it cannot pass this problem.
Better Solution
Thinking carefully, we find a property: the final answer will not exceed .
Thinking carefully again, we find that LCS has a greedy property.
Redefine as the shortest prefix length of whose longest common subsequence with the first characters of has length (that is, exchange the answer and the first-dimensional state of the naive method).
By preprocessing, for every position of , the positions of the next occurrences of , a forward transition can be performed in .
The complexity is , so this solution can pass the problem.
Example 2
Problem Statement
Given an unweighted directed graph with vertices, determine whether it contains a Hamiltonian cycle.
Naive Solution
Looking at the data range, we consider state compression.
Let indicate whether vertex can be reached from vertex by visiting only vertices in the set . Let be the adjacency matrix of the original graph. Then:
The time complexity is . It may pass if written attractively, but it is not elegant.
Better Solution
In the state design above, each value represents only one bool value, which seems wasteful.
For each state , we can consider compressing into one int. We find that after compressing the adjacency matrix in the same way, transitions can be performed in .
The time complexity is , which can pass this problem, where is the number of bits in an int.
Example 3
Problem Statement
The standard knapsack problem. is the number of items, is the knapsack capacity, and are the volume and value of item , respectively. , , .
Naive Solution
This is a standard knapsack problem.
Define as the maximum total value when the first items have been selected and the knapsack currently contains capacity .
It is easy to obtain .
, so this problem cannot be passed.
Better Solution
Swap the answer and the second dimension of the state. Let be the minimum total volume when the first items have been selected and the value of the items in the knapsack is .
Again, it is easy to obtain .
Note that after changing the second dimension of the state, the transition must be changed accordingly as well.
The time complexity is , so this solution can pass the problem.