cswork / OAOpen practice workspace

AmazonOA #179

Find Number of Compatible Substrings

Estimated HardJudge-ready

Topics (AI-labelled)Two PointersHashingStrings

Given a history string and several product strings of equal length, count the starting positions of contiguous substrings that can be formed by concatenating all product strings, each used once, in any order. Duplicate products are used as many times as they appear, and overlapping positions are counted separately.

Input

The first line contains history, the second line the number of products p, followed by p lines of product strings. This site uses lowercase strings, 1≤|history|≤100000, 1≤p≤10000, product length 1..30 with all products of equal length, and total product length ≤100000.

Output

Output a single integer, the answer.

Example 1

Input:

abcdefdefabcghidefabcxyz
3
abc
def
ghi

Output:

3

Example 2

Input:

aaaa
2
a
a

Output:

3

Example 3

Input:

abb
2
ab
ab

Output:

0

Example notes

The answers are 3, 3 and 0.

Editorial

Loading solution…