|
USACO 2.3 Longest Prefix
Typical dyanmic programming problem as the string matching can be broken into sub problems which overlap with each other.
Key observations:
- Let’s have dp[i] represents the longest possible prefix starting at index i, with i in the range of 0 to size - 1, with size being the length on the source string under match. The state transformation equation is: dp[i] = max(dp[i], dp[i] + j - i), if and only if substring starting from index i with length j - i is one of the dictionary string. With this, we search starting from the end of the string and once we finish, the answer would be dp[0].
- Compute the state transformation from end to start of string (as we are searching for longest prefix.).
Traps:
- The input string to match can span across multiple lines! So a single read will not possibly grab the entire source string; instead multiple reads might be required.
- The important constraint when matching against primitives: primitives are all in length 1..10. Without this constraints, it is very easy to get timeout error on the last test case.
|
USACO 2.2 Party Lamps
Search space would be huge so reducing and consolidating states is required. Key observations:
- Press same button twice yield no effect. Thus there is at most 2 ^ 4 = 16 switch state.
- The switch state can be further reduced by enumerating all possible switch states and consolidate. Essentially, when the count of key press is larger than three, every switch state (among the maximum 16 states in total) could possibly appear.
- The lamp state has a cycle of six.
|