6060: 良好的子字符串

内存限制:256 MB 时间限制:1 S 标准输入输出
题目类型:传统 评测方式:文本比较 上传者:
提交:7 通过:6

题目描述

  Smart Beaver最近对一个新的文字游戏产生了兴趣。要点如下:计算一些字符串的不同良好子字符串的数量。为了确定字符串是否好,游戏使用规则。总的来说有 n 条规则。每个规则由一组三个 plr) 描述,其中 p 是字符串,l 和 r (lr 是整数。我们将说字符串 t 符合规则 (p,l,r),如果字符串 p 中字符串 t 的出现次数介于 l 和 r 之间,包括 lr。例如,字符串 “ab” 符合规则 (“ab”, 1, 2) 和 (“aab”, 0, 1),但不符合规则 (“cd”, 1, 2) 和 (“abab”, 01)。
一个子字符串 s[l...r] (1≤lr≤|s|)字符串 s=s1s2...s|s||s|s) 的长度是字符串 s lsl+1...S· 将字符串 p出现的字符串 t 视为整数 l,r (1≤lr≤| 的数目对p|)这样...r]=t我们会说字符串 t 如果符合所有 n 条规则,它就是好的。聪明的海狸要求你帮助他编写一个程序,可以计算字符串的不同良好子串的数量。两个子字符串 s[x...y]s[z...w] 是不同的 iff s[x...y]≠[z...w]



Smart Beaver recently got interested in a new word game. The point is as follows: count the number of distinct good substrings of some string s. To determine if a string is good or not the game uses rules. Overall there are n rules. Each rule is described by a group of three (p, l, r), where p is a string and l and r (lr) are integers. We’ll say that string t complies with rule (p, l, r), if the number of occurrences of string t in string p lies between l and r, inclusive. For example, string "ab", complies with rules ("ab", 1, 2) and ("aab", 0, 1), but does not comply with rules ("cd", 1, 2) and ("abab", 0, 1).

substring s[l... r] (1 ≤ lr ≤ |s|) of string s = s1s2... s|s| (|s| is a length of s) is string slsl + 1... sr.

Consider a number of occurrences of string t in string p as a number of pairs of integers l, r (1 ≤ lr ≤ |p|) such that p[l... r] = t.

We’ll say that string t is good if it complies with all n rules. Smart Beaver asks you to help him to write a program that can calculate the number of distinct good substrings of string s. Two substrings s[x... y] and s[z... w] are cosidered to be distinct iff s[x... y] ≠ s[z... w].

Input

The first line contains string s. The second line contains integer n. Next n lines contain the rules, one per line. Each of these lines contains a string and two integers pi, li, ri, separated by single spaces (0 ≤ liri ≤ |pi|). It is guaranteed that all the given strings are non-empty and only contain lowercase English letters.

The input limits for scoring 30 points are (subproblem G1):

  • 0 ≤ n ≤ 10.
  • The length of string s and the maximum length of string p is  ≤ 200.

The input limits for scoring 70 points are (subproblems G1+G2):

  • 0 ≤ n ≤ 10.
  • The length of string s and the maximum length of string p is  ≤ 2000.

The input limits for scoring 100 points are (subproblems G1+G2+G3):

  • 0 ≤ n ≤ 10.
  • The length of string s and the maximum length of string p is  ≤ 50000.
Output

Print a single integer — the number of good substrings of string s.

Examples
input


aaab
2
aa 0 0
aab 1 1
output
3
input
ltntlnen
3
n 0 0
ttlneenl 1 4
lelllt 1 1
output
2
input
a
0
output
1

Note

There are three good substrings in the first sample test: "aab", "ab" and "b".

In the second test only substrings "e" and "t" are good.






输入格式

第一行包含字符串 s。第二行包含整数 n。接下来的 n 行包含规则,每行一个。这些行中的每一行都包含一个字符串和两个整数 p i,l i,r i,由单个空格 (0≤liri 分隔≤|第一|页)。保证所有给定的字符串都是非空的,并且只包含小写的英文字母。
得分 30 分的输入限制为(子问题 G1):
  • 0≤n≤10.
  • 字符串 s 的长度和字符串 p 的最大长度为 ≤200
得分 70 分的输入限制为(子问题 G1+G2):
  • 0≤n≤10.
  • 字符串 s 的长度和字符串 p 的最大长度为 ≤2000
得分 100 分的输入限制为(子问题 G1+G2+G3):
  • 0≤n≤10.
  • 字符串 s 的长度和字符串 p 的最大长度为 ≤50000

输出格式

打印单个整数 − 字符串 s 的良好子字符串的数量

输入样例 复制

aaab
2
aa 0 0
aab 1 1

输出样例 复制

3

数据范围与提示

在第一个样本测试中有三个好的子字符串:"aab"、"ab"和"b"。

在第二个测试中,只有子字符串 "e" 和 "t"是好的。