Submission #83633

# Submission time Handle Problem Language Result Execution time Memory
83633 2018-11-09T13:57:40 Z charlies_moo Mate (COCI18_mate) PyPy
40 / 100
2000 ms 23692 KB
s = raw_input().split()[0]
q = input()


def combination(n, d):
    k = d - 2
    if n - k < k:
        k = n - k
    result = 1
    for i in range(k):
        result *= n
        n -= 1
    w = k
    for i in range(w):
        result /= k
        k -= 1
    return result


def mod(num):
    return num % (10**9+7)


def lst_id(letter):
    return ord(letter) - ord("a")



arr = [[0 for i in range(26)] for i in range(len(s))]
arr_c = [0 for i in range(26)]



try:
    for i in range(len(s)-1, -1, -1):
        arr[i] = arr_c[:]
        arr_c[lst_id(s[i])] += 1

except:
    pass


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 += 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 193 ms 17492 KB Output is correct
2 Correct 159 ms 17492 KB Output is correct
3 Correct 156 ms 17492 KB Output is correct
4 Correct 225 ms 18004 KB Output is correct
5 Execution timed out 2066 ms 22636 KB Time limit exceeded
6 Execution timed out 2062 ms 23692 KB Time limit exceeded
7 Execution timed out 2082 ms 23692 KB Time limit exceeded
8 Execution timed out 2076 ms 23692 KB Time limit exceeded
9 Execution timed out 2075 ms 23692 KB Time limit exceeded
10 Execution timed out 2075 ms 23692 KB Time limit exceeded