Even substrings
Time limit2sMemory limit512 MB
For each query listing up to 5 letters, count substrings of S in which every listed letter occurs an even number of times.
- Level
Hard8 of 10
- Topics
- Bit manipulation, Hash map, Prefix sum
- Solved
- No attempts yet
Problem
You are given a string that uses only the letters a through t. Write a program that answers the following query.
k c1 c2 ... ck: print how many contiguous substrings of contain each of the letters an even number of times.
A letter that never appears occurs zero times, and zero is even. Substrings are identified by their start and end positions, so two equal substrings at different positions count separately. The empty substring is not counted.
Input
The first line has the number of test cases ().
The first line of each test case has the string . Its length is at most 100,000 and it uses only the letters a through t. The second line has the number of queries ().
Each of the next lines has one query in the form k c1 c2 ... ck. Here is a positive integer no greater than 5, and are the letters of the query. No letter appears twice in one query.
Output
For each query, print the number of substrings that satisfy it on its own line.