LISA
Time limit3sMemory limit512 MB
Given n strings and range queries, count distinct concatenations of a non-empty prefix of one string in the range with a non-empty suffix of another string in the range.
- Level
Hard9 of 10
- Topics
- Trie, String, Divide and conquer, Dynamic programming
- Solved
- No attempts yet
Problem
Ilya works at the Laboratory of Investigation of String Algorithms (LISA). He is trying to solve the following problem.
You are given an array of strings and queries. Each query is given by two integers and (). To answer a query, consider the strings that can be built as follows. Choose two strings and with . Take a non-empty prefix of and a non-empty suffix of , and concatenate them. Call such a string representable. The answer to the query is the number of distinct representable strings for and .
For example, let and , . The following strings are representable.
For , : ab = a + b, aab = a + ab, abb = ab + b, abab = ab + ab. For , : ac = a + c, aac = a + ac, abc = ab + c, abac = ab + ac. For , : ab = a + b, aab = a + ab, acb = ac + b, acab = ac + ab. For , : ac = a + c, aac = a + ac, acc = ac + c, acac = ac + ac.
So there are 12 distinct representable strings.
Input
The first line contains two integers and , the number of strings and the number of queries (). Each of the next lines contains a non-empty word made of lowercase English letters. The total length of all words does not exceed . Each of the next lines contains two integers and ().
Output
Print lines. The -th line contains the answer to the -th query.