## Deciphering the Mayan Writing

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

The first line of input contains two space-separated integers: g, the number of glyphs in W (1 ≤ g ≤ 3000) and |S|, the number of glyphs in the sequence S (g ≤ |S| ≤ 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

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

```4 11
```

### Sample Output

```2
```

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

Point Value: 15 (partial)
Time Limit: 0.50s
Memory Limit: 32M

Languages Allowed:
C++03, PAS, C, HASK, ASM, RUBY, PYTH2, JAVA, PHP, SCM, CAML, PERL, C#, C++11, PYTH3

• (0/0)
Isn't my solution O(n)? How can I make it faster?

• (2/0)
I think "TLE (wall clock)" in Java is usually a symptom of your input method being too slow.

• (1/0)
I can't seem to pass the last batch of test cases. I'm using buffered reader and it still gives me a TLE (wall clock)

• (2/0)
It looks like there are admissible Java solutions. I use BufferedReader in mine. You're probably better off switching to a faster language though.

As a note if you want to try optimizing, char arrays are faster than strings, and data structures in java.util and related packages are pretty slow.

In my AC solution, I use two int arrays and a single char array, and a single BufferedReader.

Another hint: My solution never knows what the entire string S is.

• (0/0)
Thanks for the advice, I'll try to implement them!

Edit: I implemented most of your optimizations and I got it accepted! I'll keep them in mind next time. Thanks