codeforces#P138E. Hellish Constraints
Hellish Constraints
Description
Katya recently started to invent programming tasks and prepare her own contests. What she does not like is boring and simple constraints. Katya is fed up with all those "N does not exceed a thousand" and "the sum of ai does not exceed a million" and she decided to come up with something a little more complicated.
The last problem written by Katya deals with strings. The input is a string of small Latin letters. To make the statement longer and strike terror into the people who will solve the contest, Katya came up with the following set of k restrictions of the same type (characters in restrictions can be repeated and some restrictions may contradict each other):
- The number of characters c1 in a string is not less than l1 and not more than r1.
- ...
- The number of characters ci in a string is not less than li and not more than ri.
- ...
- The number of characters ck in a string is not less than lk and not more than rk.
However, having decided that it is too simple and obvious, Katya added the following condition: a string meets no less than L and not more than R constraints from the above given list.
Katya does not like to compose difficult and mean tests, so she just took a big string s and wants to add to the tests all its substrings that meet the constraints. However, Katya got lost in her conditions and asked you to count the number of substrings of the string s that meet the conditions (each occurrence of the substring is counted separately).
The first line contains a non-empty string s, consisting of small Latin letters. The length of the string s does not exceed 105.
The second line contains three space-separated integers k, L and R (0 ≤ L ≤ R ≤ k ≤ 500).
Next k lines contain Katya's constrictions in the following form "ci li ri". All letters ci are small Latin letters, li and ri are integers (0 ≤ li ≤ ri ≤ |s|, where |s| is the length of string s). Letters ci are not necessarily different.
Print a single number — the number of substrings that meet the constrictions.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cout stream or the %I64d specificator.
Input
The first line contains a non-empty string s, consisting of small Latin letters. The length of the string s does not exceed 105.
The second line contains three space-separated integers k, L and R (0 ≤ L ≤ R ≤ k ≤ 500).
Next k lines contain Katya's constrictions in the following form "ci li ri". All letters ci are small Latin letters, li and ri are integers (0 ≤ li ≤ ri ≤ |s|, where |s| is the length of string s). Letters ci are not necessarily different.
Output
Print a single number — the number of substrings that meet the constrictions.
Please do not use the %lld specificator to read or write 64-bit integers in С++. It is preferred to use the cout stream or the %I64d specificator.
Samples
codeforces
2 0 0
o 1 2
e 1 2
7
codeforces
2 1 1
o 1 2
o 1 2
0
Note
In the first test we should count the number of strings that do not contain characters "e" and "o". All such strings are as follows (in the order of occurrence in the initial string from the left to the right): "c", "d"', "f", "r", "rc", "c", "s".
In the second test we cannot achieve fulfilling exactly one of the two identical constrictions, so the answer is 0.