s = raw_input().split()[0]
q = input()
length = len(s)+1
arr1 = [[] for i in range(length)]
arr1[0].append(1)
def get_num(row, column):
num = 0
if column >= 0:
num = arr1[row][column]
return mod(num)
def combination(n, d):
k = d - 2
return mod(arr1[n][k])
def mod(num):
return num % (10**9+7)
def lst_id(letter):
return ord(letter) - ord("a")
for j in range(1, length):
for i in range(j):
arr1[j].append(mod(get_num(j - 1, i - 1) + get_num(j - 1, i)))
arr1[j].append(1)
arr = [[0 for i in range(26)] for i in range(len(s))]
arr_c = [0 for i in range(26)]
for i in range(len(s)-1, -1, -1):
for j in range(26):
arr[i][j] = arr_c[j]
arr_c[lst_id(s[i])] += 1
arr_r = []
for i in range(q):
d, xy = raw_input().split()
total = 0
d = int(d)
x = xy[0]
y = xy[1]
for i in range(len(s)):
if s[i] == x and i >= d - 2:
total += mod(combination(i, d) * arr[i][lst_id(y)])
arr_r.append(mod(total))
for ans in arr_r:
print ans
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
115 ms |
15068 KB |
Output is correct |
2 |
Correct |
98 ms |
15068 KB |
Output is correct |
3 |
Correct |
105 ms |
15068 KB |
Output is correct |
4 |
Correct |
128 ms |
15832 KB |
Output is correct |
5 |
Correct |
395 ms |
21496 KB |
Output is correct |
6 |
Correct |
422 ms |
22784 KB |
Output is correct |
7 |
Correct |
353 ms |
22784 KB |
Output is correct |
8 |
Correct |
317 ms |
22784 KB |
Output is correct |
9 |
Execution timed out |
2073 ms |
35796 KB |
Time limit exceeded |
10 |
Execution timed out |
2062 ms |
35912 KB |
Time limit exceeded |