#K12581. Sequence Analysis
Sequence Analysis
Sequence Analysis
Alice is working on a secret project involving the analysis of sequences of characters. She is given a collection of m unique sequences (typically of length 4, although the length may vary) consisting of lowercase English letters. For each query, she is provided two letters, and her task is to determine how many of the sequences contain both of these letters.
More formally, given an integer \(m\) representing the number of sequences, followed by \(m\) strings, and then an integer \(q\) representing the number of queries, each query consists of two letters. For every query, find the number of sequences that contain both specified letters.
Example:
If the sequences are ["abcd", "bcda", "acdb", "adcb", "efgh", "ijkl"] and the queries are [("a", "b"), ("c", "d"), ("e", "f")], then the answers are 4, 4, and 1 respectively.
The relation for counting can be informally written as: \[ \text{result}_{i} = | \{ s \;:\; s \in \text{sequences} \land (\text{query}_{i1} \in s) \land (\text{query}_{i2} \in s) \} | \]
inputFormat
The input is read from standard input (stdin) in the following format:
The first line contains an integer m, the number of sequences. The next m lines each contain a non-empty string consisting of lowercase letters. The following line contains an integer q, the number of queries. The next q lines each contain two space-separated letters representing a query.
Example: 6 abcd bcda acdb adcb efgh ijkl 3 a b c d e f
outputFormat
For each query, output on a separate line a single integer representing the number of sequences that contain both specified letters. The output should be written to standard output (stdout).## sample
6
abcd
bcda
acdb
adcb
efgh
ijkl
3
a b
c d
e f
4
4
1
</p>