#K2266. Cryptic Sentence Decryption
Cryptic Sentence Decryption
Cryptic Sentence Decryption
You are given a list of cryptic sentences, each possibly containing the underscore character _
as a placeholder which can represent any lowercase letter. You are also given a series of queries. Each query provides two indices L and R representing a range of sentences (1-indexed), along with two lowercase letters a and b. For each query, your task is to determine whether there exists at least one sentence in the range that can be modified so that it contains the contiguous substring a+b
(i.e. the concatenation of a and b).
A sentence is said to contain the target substring if there is some contiguous segment of length 2 in the sentence in which every character either already matches the corresponding character in the target, or is an underscore (which can be replaced by any character). Formally, given a sentence s and a target substring t = a+b, the sentence is valid if there exists an index i (with 1 \leq i \leq |s|-1) such that for each position j (where j = 0,1), s[i+j] = t[j]
or s[i+j] = _
.
For example, consider the sentence he_lo_wor_d
and target substring he
. The segment he
at the beginning is already a match (even though later parts of the sentence contain underscores). However, for the sentence _prog_am_ing
with target substring ag
, no contiguous segment can be modified to produce ag
(since no valid pair of consecutive characters can be adjusted to match a
followed by g
).
inputFormat
The first line contains a single integer N, the number of cryptic sentences.
Each of the next N lines contains a non-empty string representing a cryptic sentence. Each sentence consists of lowercase letters and underscores (_
).
The following line contains a single integer Q, the number of queries.
Each of the next Q lines contains two integers L and R followed by two lowercase letters a and b. They represent a query that asks whether there exists a sentence in the range from the L-th to the R-th sentence (inclusive) that can be modified to contain a+b
as a contiguous substring.
outputFormat
For each query, output a single line containing YES
if there is at least one sentence in the specified range that can be modified to contain the target substring, or NO
otherwise.
3
he_lo_wor_d
_hell_cat_
_prog_am_ing
2
1 2 h e
2 3 a g
YES
NO
</p>