Problem Statement
The Resistance is a multiplayer board game. During the game each player belongs into one of two groups: some are resistance members, others are spies. In this problem there are P players, and exactly S of them are spies. The players are numbered 0 through P-1.
The game is played in rounds. In each round of the game a subset of all players goes on a mission. Each player who goes on the mission casts a secret vote on whether they want it to succeed or to fail. Resistance members always vote for the mission to succeed, and spies may cast either vote. (Sometimes a spy will vote for a mission to succeed in order to gain trust of the other players.) If at least one player on a mission voted for it to fail, the mission fails. If everybody voted for the mission to succeed, it succeeds.
You are given the
Verify whether the mission history is valid.
If there is no assignment of roles (spies / resistance members) to players that would be consistent with the given mission history, return an empty
If the mission history is valid, assume that each of the matching assignments of roles to players is equally likely.
Return a
Definition
- Class:
- Resistance
- Method:
- spyProbability
- Parameters:
- int, int, String[]
- Returns:
- double[]
- Method signature:
- double[] spyProbability(int P, int S, String[] missions)
- (be sure your method is public)
Notes
- Each element of your return value must have an absolute or a relative error smaller than 1e-9.
Constraints
- P will be between 3 and 10, inclusive.
- S will be between 1 and P, inclusive.
- missions will contain between 1 and 50 elements, inclusive.
- Each element of missions will contain exactly P+1 characters.
- For each element of missions, its first character will be 'S' or 'F', and the rest of the characters will be '0' or '1'.
- Each element of missions will contain at least one '1' character.
Examples
4
1
{"S0110", "F1100", "S0011"}
Returns: {0.5, 0.5, 0.0, 0.0 }
There is only one spy. Players 2 and 3 cannot be the spy because neither of them went on the failed mission. Players 0 and 1 can be spies. As both scenarios are valid, each of these two players is a spy with probability 50%.
4
2
{"F0100", "S0101", "F1010", "S1011"}
Returns: {0.5, 1.0, 0.5, 0.0 }
Now we have two spies. Only one player went to the first failed mission, so we can be certain that player 1 is a spy. Based on the second failed mission, one of players 0 and 2 must be a spy. Both possibilities match the rest of the input, so we assign each of them probability 50% of being a spy. Note that player 1 (a known spy) once voted for a success of a mission.
3
1
{"F110", "F101", "F011"}
Returns: { }
There is supposed to be only one spy, but no player went on all three failed missions, so the mission history is invalid.
5
2
{"F11000", "S00011", "F10100", "S01111"}
Returns: {0.8, 0.4, 0.4, 0.2, 0.2 }
One possibility is that the spies are players 1 and 2. Another possibility is that one of the spies is player 0 and the other spy can be any of the other four players.
9
3
{"S100001100", "F011110000", "F001000010", "F100010101", "S010010001", "F100001010", "F000100100"}
Returns: {0.3, 0.1, 0.4, 0.5, 0.2, 0.1, 0.6, 0.7, 0.1 }
3
1
{"F100", "F010", "S001"}
Returns: { }
Min test with -1 answer
3
1
{"S110", "S011", "F101"}
Returns: {0.5, 0.0, 0.5 }
Min test with possible assignments
10
1
{"F1111111111", "S1111111111"}
Returns: {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 }
Everybody has equal probability of being a spy (+ identical missions can have different outcomes)
10
1
{"S1111100000", "S0000011111"}
Returns: {0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1 }
No failed missions but somebody has to be a spy
10
10
{"S1111100000", "F0000011111"}
Returns: {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }
Everybody is a spy!
10
5
{"F1111100111", "S1001110100", "S0101001100", "F1101110110", "S1101111111", "S1011111111", "F1011100101", "F0010101010", "F1110001101", "F0010000101", "F1111001000", "S0010111011", "F0100000101", "F1001100110", "S0011110000", "F0000010110", "F1111110100", "F1011001101", "S0001010011", "F1010010010", "F0110000110", "S1101100001", "F0101010010", "S0110100001", "S0010111001", "F0001001011", "S0010010100", "F1111011111", "S1111010110", "S0111111011", "S0011101010", "S0011010100", "F0111110000", "F1111010110", "S0011000100", "F0111110111", "F0110101011", "F0101111101", "F1111100001", "S1111010111", "F1011101100", "F1111010111", "F0101110001", "F1000110010", "S0111100100", "F0110100101", "S0111010000", "S0010001101", "F1101110100", "F1000001101"}
Returns: {0.4339622641509434, 0.4716981132075472, 0.49056603773584906, 0.44025157232704404, 0.4276729559748428, 0.5157232704402516, 0.44654088050314467, 0.6226415094339622, 0.5974842767295597, 0.5534591194968553 }
Max missions count
10
5
{"S1111010111", "S0010000001", "S0111010100", "S1101100101", "S1000100100", "S0010101111", "S0110000010", "S1011001111", "S1001011110", "S1110110111", "S0101001010", "S1101001100", "S1111100110", "S0110011110", "S0110011101", "S1000110100", "S1010100011", "S0100011100", "S0000011101", "S1010000000", "S0110111000", "S0000001110", "S0000100111", "S1101110111", "S1011001011", "S0010000001", "S1101000110", "S0001101001", "S1011011111", "S0111011100", "S1001011110", "S0101010100", "S1010111000", "S0100001001", "S0000001011", "S0001011001", "S0010111011", "S1111011101", "S1011000100", "S0011001100", "S0000101111", "S0001101011", "S0111101010", "S0000010100", "S1001001100", "S1010000100", "S1101101011", "S0000100010", "S0010110101", "S1001111011"}
Returns: {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 }
Max number of missions + max number of valid assignments
10
5
{ "F0100111111", "F0011111111", "F0001101110", "F1001000111", "S1101110100", "F1010111101", "F1000100001", "F0101110000", "F1010110100", "F1001111111", "F1011001111", "S0100001110", "F1011110000", "F1111100011", "F0000110010", "F1100111111", "S1110110100", "F1111011111", "F1110101111", "S0100111101", "F1110010111", "F0100011001", "F1101110111", "F1110111000", "F0110111011", "F1110011110", "F1000111100", "F1001111110", "F1010111001", "S1111110111", "F0100000110", "F1010111111", "F1100010011", "F0111111111", "S1001100111", "F0011111110", "F1111011011"}
Returns: {0.5, 0.5287356321839081, 0.40229885057471265, 0.43103448275862066, 0.5977011494252874, 0.5402298850574713, 0.43103448275862066, 0.4942528735632184, 0.5632183908045977, 0.5114942528735632 }
8
4
{ "F10111110", "S11110111", "F01110010", "F11100110", "F11111110", "F01111011", "F11110110", "F11101110", "S11111101", "F11001110", "S11011110", "F10111111", "F11111011", "F10111111", "F10110111", "S11111111", "S11111111", "F11011011", "F11111100", "F10111011", "S01111111", "F01111111", "F11111101", "F00111110", "S11110111", "F10111111", "S11110111", "F10101011", "F11011111", "F11111100", "F01001111", "F11111110", "F10111110", "F10101111", "F11111101", "F11101011", "S01111111", "F10101010"}
Returns: {0.5, 0.5, 0.5147058823529411, 0.5, 0.5, 0.4852941176470588, 0.5147058823529411, 0.4852941176470588 }
8
2
{ "F11000011", "F11100010", "S00010100", "F01110111", "F00100011", "S00110101", "F11011100", "S01000000", "F01000111", "S01011000", "S10001001", "S01011000", "F01001101", "S11011101", "S11111001", "F11110001", "S10001101", "F01011001", "F01011111", "F11010011", "F11010010", "F00011111", "F01110110", "F10000000", "F01111001", "S01111001", "S11001101", "S01111011", "S11011010", "S01011011", "S00000110", "S11101110", "S01111100", "F11001101", "S11110110", "F11010111", "S10010000", "F00010010"}
Returns: { }
9
5
{ "S110101100", "S110001001", "F011001010", "F010010011", "F010000101", "F100001011", "F100100011", "F000010000", "F101110011", "S001001011", "F001010100", "S100010011", "F011011110", "F001111101", "F100111101", "S101100010", "F111000000", "F101110101", "S011111100", "F111100101", "F001101011", "S110000100", "F011000111", "F100000011", "F000001000", "F110111110", "F000101111", "F110010011", "F110011000", "F001110010", "S110110110", "F110010000", "F110100011", "F100101100", "S111110100", "F000110000", "S011000111", "F011011000"}
Returns: {0.5217391304347826, 0.5217391304347826, 0.391304347826087, 0.2608695652173913, 1.0, 1.0, 0.391304347826087, 0.391304347826087, 0.5217391304347826 }
7
6
{ "S1010100", "F1000101", "S0111111", "S1100011", "F1010111", "S1111111", "S1010110", "F1011101", "F0101011", "S1001111", "S1001111", "S1110111", "S1100100", "F1011101", "S1110111", "S1111110", "F1011111", "F1111011", "S1110001", "S1001010", "S1101111", "F1010000", "F1111100", "F1111101", "S1011010", "F0110111", "S0011110", "F1001101", "S0111110", "F1001111", "F0010000", "F0100101", "S1110010", "S1001011", "F1110101", "F0111111", "F0110101", "S1010101", "S1001100", "F1111010", "F1111001", "S0000110"}
Returns: {0.8333333333333334, 0.8333333333333334, 1.0, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334 }
5
2
{ "F00110", "S00100", "F10010"}
Returns: {0.4, 0.2, 0.4, 0.8, 0.2 }
6
5
{ "F011101", "F110100", "F000100", "F000001", "S000010", "F100100", "S001000", "F001010", "S000010", "S100000"}
Returns: {0.75, 0.75, 0.75, 1.0, 0.75, 1.0 }
8
4
{ "F11111111", "F11111111", "F11101100", "F11111111", "F01011111", "S11111111", "S11111111", "S11111111", "S11111011", "S10101111", "S11111111", "F11111111", "F11110111", "S11111111", "S11111110", "F11111110"}
Returns: {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 }
7
3
{ "F1111111", "F1111100", "F1011111", "F0010101", "F1111110", "F0010110", "F0010000", "F0101111", "F1001110", "F1110111", "F0001011", "F0111101", "F1110111", "F1000011", "F1101010", "F1111011", "F1111001", "F1011011", "F1000011", "F0000011", "F1110001", "F1111111", "F1010111"}
Returns: {0.2857142857142857, 0.14285714285714285, 1.0, 0.2857142857142857, 0.14285714285714285, 0.7142857142857143, 0.42857142857142855 }
10
7
{ "S1111101111", "F1100111101", "F0011111111", "S1011111111", "F1110110111", "F1111111111", "S0111110110", "F1100111111", "S0111101110", "F1111111110", "S1111010111"}
Returns: {0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7, 0.7 }
4
3
{ "S1111", "F0110", "F0010", "F1110", "F1110", "F0100", "F1010", "F1110", "F1011", "F0111", "F1111", "F1011", "F1101", "F0011", "F0001", "F1110", "F1010", "F1111", "F1111", "F1110", "F1110", "F1101", "F1101", "F0111", "F1111", "F1111", "F1111", "F1110", "F0110", "F1111", "F0101", "F1101", "F1110", "S1001", "F1101", "F0111", "F1111", "F1111", "F0011", "F0111", "F1111", "F1101", "F1111", "F0110"}
Returns: {0.0, 1.0, 1.0, 1.0 }
5
3
{ "F11111", "F11010", "S01111", "F01111", "F11101", "S01101", "F00111", "F00011"}
Returns: {0.5555555555555556, 0.5555555555555556, 0.5555555555555556, 0.6666666666666666, 0.6666666666666666 }
6
5
{ "F010101", "F101100", "F111101", "S111000", "F101000", "F111011", "F001011", "S110001", "F111111", "F111111", "F111011", "F101010", "F101011", "F110100", "F101101", "F010011", "F111111", "S101111", "F101010", "S110010", "S110100", "S100001", "F111011", "F101111", "F111001", "F001010", "F010101", "S110111", "F111001", "S010111", "F101000", "F000011", "S011100", "S001111", "F001001", "F011111", "F111111", "S111011", "F101100", "F111111", "F100110", "S010110", "S110001", "F111111"}
Returns: {0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334 }
5
3
{ "F10100"}
Returns: {0.6666666666666666, 0.5555555555555556, 0.6666666666666666, 0.5555555555555556, 0.5555555555555556 }
6
4
{ "F110000", "F110010"}
Returns: {0.7142857142857143, 0.7142857142857143, 0.6428571428571429, 0.6428571428571429, 0.6428571428571429, 0.6428571428571429 }
9
6
{ "F110001111", "F100000010", "F000101011", "F101000010", "F010100110", "F011101001", "F110011101", "F010001000", "F101001001", "F001001110", "F001001110", "F000000001", "F001001010", "F010000000", "F010000100", "F000010010", "F110011110", "F110001001", "F000101100", "F000110011", "F001100011", "F000000001", "F011111110", "F110000110", "F010001001", "F000000001", "F000001001"}
Returns: {0.5833333333333334, 1.0, 0.5, 0.5, 0.5833333333333334, 0.5416666666666666, 0.5, 0.7916666666666666, 1.0 }
9
4
{ "F111111110", "S111111100", "F111111111", "F111111010", "F111111111", "F001110011", "F110111111", "F111111011"}
Returns: {0.44, 0.44, 0.448, 0.448, 0.448, 0.44, 0.44, 0.448, 0.448 }
7
3
{ "S0010111", "F1010111", "F1111100", "F1010111", "S1111100", "F1111011", "S0010111", "S1000100", "F1101011", "F1111111", "F0010111", "F1100001", "S0111111", "F0000111", "F0001111", "S1111101", "F1110111", "F1111110"}
Returns: {0.4444444444444444, 0.4444444444444444, 0.3333333333333333, 0.3333333333333333, 0.4444444444444444, 0.4444444444444444, 0.5555555555555556 }
6
5
{ "F111111", "F101011"}
Returns: {0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334 }
9
7
{ "F001101110", "F111101101", "S011001111", "F110100110", "F010010000", "F100100101", "F011011100", "F101100010", "S000011100", "F101100111"}
Returns: {0.7714285714285715, 0.8, 0.7714285714285715, 0.7714285714285715, 0.8, 0.7714285714285715, 0.7714285714285715, 0.7714285714285715, 0.7714285714285715 }
7
3
{ "F0110010", "F0111100", "F0111100", "F0011111", "F1100000", "F1000000", "F1010001", "F1000011", "F0011101", "F0100111", "F0010010", "F0110001", "F0100100", "S1110000", "F0111011", "F0011110", "S1100001", "F0110011", "F0100001", "F0110111", "F1000001", "F1111010", "F0001000", "F1001011", "F0000010", "F0110100"}
Returns: { }
10
5
{ "S1111110011", "S0101100011", "S1100101100"}
Returns: {0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5, 0.5 }
8
4
{ "F10101101", "F00110001", "S10001011", "S01111001", "F01000011", "F10110111", "F11101100", "F11111101", "S11000110", "F10011010", "F00111110", "S11100000", "F01110010", "F11000010", "F11001100", "F00101011", "F00110100", "F10100010", "S10101101", "F01001110", "S11011010", "F10111101", "F11101011", "F10111111", "F10010000", "S00001110", "S10000111", "F11001011", "F00111100", "F10111000", "S01000001", "F01010111", "F10111100", "F00010100", "S11110010", "F00100000", "S10100111", "F10100001", "F11011111"}
Returns: {0.45454545454545453, 0.5454545454545454, 1.0, 0.7272727272727273, 0.18181818181818182, 0.45454545454545453, 0.45454545454545453, 0.18181818181818182 }
8
4
{ "F10101101", "S10000011", "S11110111", "S10111110", "F01100110", "F11111011", "F11101011", "F01001000", "F10001001", "S11100110", "S01011101", "S00100110", "F01100100", "S10010100", "F11100010", "S00010111", "F00110111", "F00000111", "S11111110", "S01011111", "F00111111", "F10100110", "F01011110", "S00110100", "F01110011", "F11111101", "S00100111", "F01000101", "S11110101", "S11001011", "F11100111", "S00000100", "F00111111", "S00100001", "S01000010", "F11101100", "F01100101", "S10001001", "F00011110", "F01101111", "F00000101", "F00101111"}
Returns: {0.40625, 0.625, 0.40625, 0.3125, 0.625, 0.65625, 0.375, 0.59375 }
7
6
{ "F1001000", "S1101101", "S1101111", "S1001011", "F1000111", "F0011111", "F1011110", "S0101001", "S1111001", "F1101101", "F1010000", "S0101111", "S0110011", "S1110101", "F0001101", "F1001101", "S0010101", "F1101001", "S0010000", "S0111100", "F1011010", "S1000110", "S0001000", "F1101110", "F1110010", "S1111100", "F0101101", "F0001001", "S1011111", "F1000011", "S1101011", "S1111011", "F0110001", "S0111001", "S0001011", "F0101101", "S1111001", "S1111110", "F1001101", "S1101100", "S0100001", "S0001110", "F0000001", "S1000101"}
Returns: {0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 0.8333333333333334, 1.0 }
8
3
{ "F01111100", "S00101111", "F01001001", "S11100010", "F00000110", "F00111011", "F11010100", "F00000110", "F00001110", "F00110001", "S11001001", "S00110000", "S01100010", "S10110110", "S00010010", "S00011001", "S11011111", "S01100000", "F00100000", "S01001000", "S11001110", "F10110100", "S00000010", "S10000000", "F00000010", "F10001000", "F10000000", "F01001001", "F01010000", "S11100000", "F00100101", "F01010001", "S00111110", "S10000101"}
Returns: { }
7
6
{ "F1001111", "F1111110", "F1000001"}
Returns: {0.8571428571428571, 0.8571428571428571, 0.8571428571428571, 0.8571428571428571, 0.8571428571428571, 0.8571428571428571, 0.8571428571428571 }
6
4
{ "F011110", "F100110", "F101111", "F011001", "F111001", "F000101", "F010111", "F100101", "F001001", "F111010", "F001111"}
Returns: {0.6153846153846154, 0.6153846153846154, 0.6923076923076923, 0.6923076923076923, 0.6153846153846154, 0.7692307692307693 }
7
4
{ "F0100010", "F0100010", "S1000110", "F0000011", "F1001000", "S0000111", "S0100010", "S0010001", "S0100000", "S0000110", "F1010001"}
Returns: {0.65, 0.55, 0.45, 0.6, 0.4, 0.75, 0.6 }
10
6
{ "S1000111000", "S0110101111", "F0001100010", "S1110011011", "S0001011010", "F0000011010", "S0101111111", "S1111101110", "S0000001010", "S0010001111", "F1110110111", "S1000010110", "F0010011010", "S1110011101", "S0001000000", "S1111101000", "S1111000110", "S0000000111", "F1101011100", "F1110100010", "S1101011111", "F0100001100", "S0101011101", "F1011001110", "F1000101000", "S0001011110", "F0010001101", "F1101011001", "F1010000111", "S1000101001", "S0010011111", "F0111000110", "S0111110001", "S1111001010", "S1010100010", "F0110010101"}
Returns: {0.5911602209944752, 0.5911602209944752, 0.56353591160221, 0.5911602209944752, 0.6243093922651933, 0.5911602209944752, 0.6629834254143646, 0.5966850828729282, 0.6243093922651933, 0.56353591160221 }
6
5
{ "S000110", "S000010", "F101111", "S100100", "F001011", "S010000", "S100000", "F010101", "S000100", "F001011", "S111000", "F010011", "F010000", "S100100"}
Returns: {0.8, 1.0, 0.8, 0.8, 0.8, 0.8 }
5
3
{ "S10110", "S11011", "S11001", "F00011", "F01111", "S11100", "S10111", "F00100", "S10111", "F10111", "S10010", "F11101", "F11110", "S11011", "F11101", "S01111", "S10100", "F11111", "F11101", "S10101"}
Returns: {0.4, 0.4, 1.0, 0.6, 0.6 }
7
3
{ "F0100111", "F0110110", "F1010101", "F1111111", "F1011111", "F0101001", "F1110111", "F1000100", "F0100101", "F1110011", "F1100001", "F1010110", "F1101011", "F1101101", "F0111101", "F1111100", "F0111000", "F0011010", "F1101101", "F1010110", "F1011111", "S1100110", "F1011001", "F1100110", "F0111101", "F0111001", "F1000001", "F0011000", "F0101110", "F0111111", "F1101110", "F1111110", "F1001111", "F1011110", "F0110101", "F1011101", "F1111111"}
Returns: {0.6, 0.4, 0.4, 0.6, 0.6, 0.0, 0.4 }
9
7
{ "F111011011", "F000100111", "S000011110", "F001011000", "S011101110", "S111000010"}
Returns: {0.7777777777777778, 0.7777777777777778, 0.7777777777777778, 0.7777777777777778, 0.7777777777777778, 0.7777777777777778, 0.7777777777777778, 0.7777777777777778, 0.7777777777777778 }
7
5
{ "F0100100", "F0001011", "F0000011"}
Returns: {0.6842105263157895, 0.7368421052631579, 0.6842105263157895, 0.6842105263157895, 0.7368421052631579, 0.7368421052631579, 0.7368421052631579 }
10
7
{ "F1001110101", "F0101111010", "F0000011011", "F0111011101", "F1010101111", "F1000101000", "F0011111100", "F1111101100", "F0100111000", "F1010011110"}
Returns: {0.7058823529411765, 0.6974789915966386, 0.6974789915966386, 0.6974789915966386, 0.7058823529411765, 0.6974789915966386, 0.7058823529411765, 0.6974789915966386, 0.6974789915966386, 0.6974789915966386 }
7
2
{ "F1100010", "F0101010", "F0110110", "F1001001", "F0111011", "F0000100", "F1100011", "F1100001", "F1011001"}
Returns: { }
7
2
{ "F1100110", "S0010000", "F0011100", "F1011010", "F0001001", "F1100010"}
Returns: {0.3333333333333333, 0.3333333333333333, 0.0, 1.0, 0.0, 0.3333333333333333, 0.0 }
8
2
{ "F01111100", "F00001010", "F01100101", "F10000000", "F01011111", "F00110000", "F10101111", "F11011011", "S11101011"}
Returns: { }
8
4
{ "S10101011", "S01010111", "S10110110", "F10100111", "F10010010", "S10110111", "F11001111", "F00000001"}
Returns: {0.4838709677419355, 0.3870967741935484, 0.3870967741935484, 0.4838709677419355, 0.3870967741935484, 0.3870967741935484, 0.4838709677419355, 1.0 }
5
4
{ "F01111", "F11011", "F00101", "S11010", "F11111"}
Returns: {0.8, 0.8, 0.8, 0.8, 0.8 }
8
5
{ "F00100000", "F01001011", "S01001111", "S00100111", "F00100110", "F00110000", "F00011101"}
Returns: {0.5714285714285714, 0.5714285714285714, 1.0, 0.5714285714285714, 0.5714285714285714, 0.5714285714285714, 0.5714285714285714, 0.5714285714285714 }
4
1
{"S0110", "F1100", "S0011" }
Returns: {0.5, 0.5, 0.0, 0.0 }
3
3
{"S111" }
Returns: {1.0, 1.0, 1.0 }
4
2
{"F0100", "S0101", "F1010", "S1011" }
Returns: {0.5, 1.0, 0.5, 0.0 }
7
1
{"F1111111" }
Returns: {0.14285714285714285, 0.14285714285714285, 0.14285714285714285, 0.14285714285714285, 0.14285714285714285, 0.14285714285714285, 0.14285714285714285 }