Oulipo
InterviewTime limit1sMemory limit128 MB
Count overlapping occurrences of a pattern W in a text T over uppercase letters for each test case.
- Level
Medium4 of 10
- Topics
- String matching, String
- Solved
- No attempts yet
Problem
The French writer Georges Perec (1936-1982) wrote a whole novel, La disparition, without ever using the letter 'e'. He belonged to the Oulipo group. A passage from the book:
Tout avait l’air normal, mais tout s’affirmait faux. Tout avait l’air normal, d’abord, puis surgissait l’inhumain, l’affolant. Il aurait voulu savoir où s’articulait l’association qui l’unissait au roman : sur son tapis, assaillant à tout instant son imagination, l’intuition d’un tabou, la vision d’un mal obscur, d’un quoi vacant, d’un non-dit : la vision, l’avision d’un oubli commandant tout, où s’abolissait la raison : tout avait l’air normal mais…
Perec would have scored high, or rather low, in the contest below. Entrants write a text on a given subject while making one given word appear as few times as possible. The jury needs a program that counts those appearances so the entrants can be ranked. Entrants often hand in very long texts with no meaning at all. A run of 500,000 consecutive 'T's is not unusual, and they never write spaces.
So we have to count quickly how often one string occurs inside another. Over the alphabet {'A', 'B', 'C', …, 'Z'} you are given a word and a text . Count how many times occurs in . Every character of must match consecutive characters of exactly. Occurrences may overlap.
Input
The first line contains the number of test cases. Each test case has this format:
- One line with the word , a string over {'A', 'B', 'C', …, 'Z'}, with . Here is the length of .
- One line with the text , a string over the same alphabet, with .
Output
For each test case, print the number of occurrences of in on a line of its own.