IOI '06 P1 - Deciphering the Mayan Writing

View as PDF

Submit solution


Points: 7 (partial)
Time limit: 0.3s
Memory limit: 32M

Problem type
IOI '06 - Merida, Yucatan, Mexico

Deciphering the Mayan writing has proven to be a harder task than anticipated by the early investigations. After almost two hundred years, very little of it was actually understood. It has been only in the last three decades that real advances have been made.

Mayan writing is based on small drawings known as glyphs which represent sounds. Mayan words are normally written as glyphs put together at various positions.

One of several problems in deciphering Mayan writing arises in the order of reading. When placing several glyphs in order to form a word, Mayan writers sometimes decided the position based more on their own esthetic views than on any particular rule. This leads to the fact that, even though the sound for many glyphs is known, sometimes archaeologists are not sure how to pronounce a written word.

The archaeologists are looking for a special word W. They know the glyphs for it, but they don't know all the possible ways of arranging them. Since they knew you were coming to IOI '06, they have asked for your help. They will provide you with the g glyphs from W and a sequence S of all the glyphs (in the order they appear) in the carvings they are studying. Help them by counting the number of possible appearances of the word W.

Write a program that, given the glyphs for W and the sequence S of glyphs in the carvings, counts the number of possible appearances of W in S; that is, every sequence of consecutive g glyphs in S that is a permutation of the glyphs in W.

Input Specification

The first line of input contains two space-separated integers: g, the number of glyphs in W (1 \le g \le 3\,000) and |S|, the number of glyphs in the sequence S (g \le |S| \le 3\,000\,000).

The second line contains g consecutive characters that represent the glyphs in W. Valid characters are the uppercase and lowercase characters of the Roman alphabet. Uppercase and lowercase characters are considered to be different.

The third line contains |S| consecutive characters that represent the glyphs in the carvings. Valid characters are the uppercase and lowercase characters of the Roman alphabet. Uppercase and lowercase characters are considered to be different.

Output Specification

One integer on a single line, the number of possible appearances of W in S.

Grading

In some test cases worth 50\% of the total marks, g will be no more than 10.

Sample Input

4 11
cAda
AbrAcadAbRa

Sample Output

2

Comments

There are no comments at the moment.