LCS

3.5

20 votes
Very-Easy
Problem

Biswa and Debu are playing a weird word game. Biswa has a set S of N strings. Debu makes Q turns. In each turn he gives Biswa a string T.
In each turn, Biswa's task is to find the longest substring of T which is also a substring in one of the strings in S.

Note: You can find the definition of substring here.

Input

  • The first line contains a single integer N
  • Next N lines describe the strings in S. Each line contains a single string
  • Next line contains a single integer Q
  • Next Q line contain one string each
Output
  • For each turn, you need to output a single integer which is the answer to that turn in a new line
Constraints
  • 1 ≤ N ≤ 103
  • Sum of lengths of all the strings in S ≤ 2 * 105
  • 1 ≤ Q ≤ 103
  • Sum of lengths of the strings over all turns ≤ 2 * 105
  • All the strings contain only lowercase English alphabets

Sample Input
3
aaa
bbc
bac
3
aa
ccb
cba
Sample Output
2
1
2
Time Limit: 1
Memory Limit: 256
Source Limit:
Editor Image

?