#P8630. [蓝桥杯 2015 国 B] 密文搜索

[蓝桥杯 2015 国 B] 密文搜索

Description

Holmes received a document from Planet X, consisting entirely of lowercase letters.

His assistant provided another document: a list of many passwords of length 88.

Holmes found that these passwords were shuffled and hidden in the previous document.

Please write a program to search, in the first document, for positions where hidden passwords may appear. You must consider all permutations of each password.

Input Format

The first line contains a string ss, consisting only of lowercase letters, with length less than 1024×10241024 \times 1024.

The next line contains an integer nn, indicating that there are nn lines of passwords below, where 1n10001 \le n \le 1000.

The next nn lines each contain a string, all consisting of lowercase letters, each of length 88.

Output Format

Output one integer, representing the total number of matches in ss of all permutations of the password on each line.

aaaabbbbaabbcccc
2
aaaabbbb
abcabccc
4

Hint

The first password matches 33 times, and the second password matches 11 time, for a total of 44 times.

Time limit: 3 seconds, 512 MB. Lanqiao Cup 2015, 6th National Finals.

Translated by ChatGPT 5