s = raw_input()
q = input()
def combination(n, d):
k = d - 2
result = 1
for i in range(k):
result*=n
n-=1
for i in range(k):
result/=k
k-=1
return result
def mod(num):
return num % (10**9+7)
arr = []
for i in range(q):
d, xy = raw_input().split()
d = int(d)
x = xy[0]
y = xy[1]
arr_l = []
arr_c = []
a = -1
b = -1
if x != y:
for i in range(len(s)):
if i >= d - 2 and s[i] == x:
arr_l.append(i)
arr_c.append(0)
a = i
b += 1
elif len(arr_l)>0 and s[i]==y and i > a:
for k in range(len(arr_c)):
arr_c[k] += 1
for j in range(len(arr_l)):
arr_l[j] = mod(combination(arr_l[j], d))
total = 0
for i in range(len(arr_l)):
total += mod(arr_l[i]*arr_c[i])
arr.append(mod(total))
else:
for i in range(len(s)):
if i >= d - 2 and s[i] == x:
arr_l.append(i)
for k in range(len(arr_c)):
arr_c[k] += 1
arr_c.append(0)
for j in range(len(arr_l)):
arr_l[j] = mod(combination(arr_l[j], d))
total = 0
for i in range(len(arr_l)):
total += mod(arr_l[i]*arr_c[i])
arr.append(mod(total))
for i in arr:
print i
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
370 ms |
21568 KB |
Output is correct |
2 |
Correct |
326 ms |
21568 KB |
Output is correct |
3 |
Correct |
298 ms |
21568 KB |
Output is correct |
4 |
Correct |
387 ms |
21568 KB |
Output is correct |
5 |
Execution timed out |
2049 ms |
21568 KB |
Time limit exceeded |
6 |
Execution timed out |
2052 ms |
21568 KB |
Time limit exceeded |
7 |
Execution timed out |
2074 ms |
21568 KB |
Time limit exceeded |
8 |
Execution timed out |
2045 ms |
21568 KB |
Time limit exceeded |
9 |
Execution timed out |
2058 ms |
21568 KB |
Time limit exceeded |
10 |
Execution timed out |
2060 ms |
21568 KB |
Time limit exceeded |