CaptureFish
SRM 539 · 2011-11-22 · by ir5
Problem Statement
In this problem, regard the pond as a 2-dimensional Cartesian plane (as seen from above). Each fish and each buoy is a point on the plane. The buoys are lined up on x axis and numbered 0 to N from left to right. There is exactly one fish between each two neighboring buoys. The fish are numbered 0 to N-1 from left to right. For the purpose of this problem we will assume that the fish are staying on their spots without any movement. The exact coordinates of the fish and the buoys do not matter. See the following image for clarity.
You are given a
To capture the fish, Mr. Jeipouju wants to set up a net in the pond so that the net will separate the caught fishes from the uncaught ones. From above, the net must be a closed curve in our plane. Furthermore, this closed curve must satisfy the following conditions:
- The net is not allowed to pass through any of the fish.
- The net is not allowed to touch or intersect itself.
- The net may only cross the x axis at points that contain the buoys. The net is not allowed to touch the x axis without crossing it.
- The net must cross the x axis at least twice.
- The fish Mr. Jeipouju wants to capture and the fish he wants not to capture must be separated by the net. That is, either all fish marked with 'X' are to be inside the net and all fish marked with 'O' outside, or vice versa. The fish marked '*' may be anywhere, possibly some of them inside and some outside the net.
A net can be encoded into a sequence using the following algorithm:
- Start anywhere on the net, but not on a buoy.
- Walk along the net until you reach your starting point again.
- During the walk, each time you encounter a buoy, write down its number and the halfplane in which you are moving away from the x axis. (The halfplane is "+" if after visiting the buoy your y coordinate is positive and "-" if it is negative.)
Mr. Jeipouju wants to know whether the number of different nets is odd or even. Your method must return the number of different nets, modulo 2.
Constraints
- fish will contain between 1 and 50 characters, inclusive.
- Each character of fish will be either letter 'O' or 'X' or '*'.
- fish will contain at least one 'O' character.
"OXOXO" Returns: 0
In this case, there are 5 fish. There are 8 ways to separate them.
"OO" Returns: 1
There is only one valid net and it looks as follows: Two things to notice: First, the net does not have to pass through all the buoys. Second, it is allowed to have no fish at all at either side of the net.
"**OX**" Returns: 0
"O***O***O***O" Returns: 1
"O*X**X***X*O" Returns: 0
Submissions are judged against all 86 archived test cases, of which 5 are shown here. Case numbers match the judge’s.
Language: C++17 · define a public class CaptureFish with a public method int getParity(string fish) · 86 test cases · 2 s / 256 MB per case