JOI '21 Open P1 - Crossing
View as PDFDo you know Just Odd Investigations Laboratory? The business of this laboratory is to do "just odd investigations." In the following, we call it JOI Laboratory for short.
In recent years, broad gardens with splendid flowers were discovered in several historical ruins in the world.
JOI Laboratory discovered that the flowers in these gardens are new species, and their genes have similar features. The genes of these new flowers are strings of length  consisting of 
J, O, I. These strings are called gene sequences.
You are a researcher working in JOI Laboratory. You initially have three flowers of new species. Their gene sequences are , 
, 
, respectively.
You can obtain a flower of new species from two flowers of new species by an operation called crossing. The -th character 
 of the gene sequence of the new flower is determined by the following rule.
- If the 
-th characters of the gene sequences of the two flowers are the same, the
-th character of the gene sequence of the new flower will be the same character.
 - If the 
-th characters of the gene sequences of the two flowers are different, the
-th character of the gene sequence of the new flower will be one of
J,O, orIwhich is different from these two characters. 
In other words, if the -th characters of the gene sequences of the two flowers are 
 and 
, the 
-th character 
 of the gene sequence of the new flower is given by the following table.
| J J  | 
        J O  | 
        J I  | 
        O J  | 
        O O  | 
        O I  | 
        I J  | 
        I O  | 
        I I  | 
    |
| J | I | O | I | O | J | O | J | I | 
You may use the same flower for crossing as many times as you want. If you obtain a new flower by crossing, you may use it for subsequent crossings.
In order to obtain more beautiful flowers, JOI Laboratory proposed  gene sequences numbered from 
 to 
 as candidate gene sequences. You are given a list describing the candidate gene sequences. The list contains a string 
 and, for each 
 
, integers 
, 
 and a character 
. The candidate gene sequences are given as follows:
- The candidate gene sequence 
is
.
 - The candidate gene sequence 
is obtained from the candidate gene sequence
by replacing the characters from the
-th position to the
-th position by the character
.
 
Write a program which, given an integer , the gene sequences of the three initial flowers, and the list describing the candidate gene sequences, determines, for each candidate gene sequence, whether it is possible to obtain a flower whose gene sequence is the given one from the three initial flowers by crossing zero or more times.
Input Specification
Read the following data from the standard input:
Output Specification
Write  lines to the standard output. In the 
-th line 
, output 
Yes if it is possible to
obtain a flower whose gene sequence is the candidate gene sequence  from the three initial flowers by crossing
zero or more times. Otherwise, output 
No.
Constraints
.
,
,
are strings of length
. Each character is either
J,O, orI..
is a string of length
. Each character is either
J,O, orI..
is either
J,O, orI.
Subtasks
- (3 points) 
,
.
 - (23 points) 
.
 - (23 points) 
.
 - (51 points) No additional constraints.
 
Sample Input 1
4
JOJO
JJOI
OJOO
3
IJOJ
1 4 O
2 2 J
2 4 I
Sample Output 1
Yes
No
Yes
Yes
Explanation for Sample Output 1
The gene sequences of the three initial flowers are JOJO, JJOI, and OJOO. In the following, new flowers obtained by crossing are written as their gene sequences.
is
IJOJ. Since it is possible to obtainIJOJby crossingJJOIwithOJOO, outputYes.is
OOOO. Since it is impossible to obtainOOOOfrom the three initial flowers by crossing zero or more times, outputNo.is
OJOO. Since you haveOJOOin the beginning without crossing, outputYes.is
OIII. You can obtainIJOJby crossingJJOIwithOJOO. Then you can obtainOIIIby crossingJOJOwithIJOJ. Therefore, outputYes.
This sample input satisfies the constraints of Subtasks 3, 4.
Sample Input 2
3
JOI
JOI
JOI
2
OJI
1 2 O
1 1 J
Sample Output 2
No
No
Yes
Explanation for Sample Output 2
The gene sequences of the three initial flowers are JOI only. You can obtain JOI only by crossing.
is
OJI. Since it is impossible to obtain this flower by crossing, outputNo.is
OOI. Since it is impossible to obtain this flower by crossing, outputNo.is
JOI. Since it is possible to obtain this flower, outputYes.
This sample input satisfies the constraints of Subtasks 1, 2, 3, 4.
Comments