Problem:


Score : 300 points

Problem Statement

You are given a string S of length N consisting of A, C, G and T. Answer the following Q queries:

Query i (1 \leq i \leq Q): You will be given integers l_i and r_i (1 \leq l_i < r_i \leq N). Consider the substring of S starting at index l_i and ending at index r_i (both inclusive). In this string, how many times does AC occurs as a substring?

Notes

A substring of a string T is a string obtained by removing zero or more characters from the beginning and the end of T.

For example, the substrings of ATCODER include TCO, AT, CODER, ATCODER and  (the empty string), but not AC.

Constraints

2 \leq N \leq 10^5

1 \leq Q \leq 10^5

S is a string of length N.

Each character in S is A, C, G or T.

1 \leq l_i < r_i \leq N

Input

Input is given from Standard Input in the following format:

N Q
S
l_1 r_1
:
l_Q r_Q

Output

Print Q lines. The i-th line should contain the answer to the i-th query.

Sample Input 1

8 3
ACACTACG
3 7
2 3
1 8

Sample Output 1

2
0
3

Query 1: the substring of S starting at index 3 and ending at index 7 is ACTAC. In this string, AC occurs twice as a substring.

Query 2: the substring of S starting at index 2 and ending at index 3 is CA. In this string, AC occurs zero times as a substring.

Query 3: the substring of S starting at index 1 and ending at index 8 is ACACTACG. In this string, AC occurs three times as a substring.



Sample input:
8 3
ACACTACG
3 7
2 3
1 8


Sample output:
2
0
3

Submission status: Runtime Error

Solution code:
N, Q = map(int, input().split())

S = str(input())

L = []
for i in range(Q):
    l,r = map(int, input().split())
    L.append((l-1,r-1))
    
    
check = []
for i in range(N-1):
    if S[i:i+2] == "AC":
        check.append(i)
        
for i in L:
    print(bisect_right(check, i[1]-1)-bisect_left(check, i[0]))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
s = input()
while True:
    l, r = map(int, input().split())

    k = 0

    for i in range(l, r + 1, 2):
        if s[i - 1:i + 1] == "AC":
            k += 1

        if i + 2 < r:
            if s[i:i + 2] == "AC":
                k += 1

    print(k)

Submission status: Runtime Error

Solution code:
N,Q=map(int,input().split())
S=input()
L = [list(map(int,input().split())) for i in range(Q)]

P=[]
for k, j in enumerate(S):
        if j=="A" and S[k+1]=="C":
            P.append(k+1)
print(P)

for i in L:
    flag=0
    res=0
    for j in P:
        if(j>=i[0] and j<i[1]):
            res+=1
        else:
            if(flag==1):
                break
            flag=1
    print(res)
'''
for i in L:
    res=0
    S_ = S[i[0]-1:i[1]]
    for k, j in enumerate(S_[:-1]):
        if j=="A" and S_[k+1]=="C":
            res+=1
    print(res)
'''

Submission status: Runtime Error

Solution code:
import re
N,Q = input().split()
S = input()
for _ in range(int(Q)):
    l,r = input().split()
    print(len(re.findall(r'AC', S[l+1:r+2])))

Submission status: Runtime Error

Solution code:
def main():
    N, Q = map(int, input().split())
    S = input('')
    ac_list = []
    for i in range(Q):
        l, r = map(int, input().split())
        S_cut = S[l - 1:r]
        result = S_cut.count('AC')
        ac_list.append(result)

    for ans in result:
        print(ans)


if __name__ == '__main__':
    main()

Submission status: Runtime Error

Solution code:
import bisect
I=lambda:map(int,input().split())
N,Q=I()
S=input()
m=[]
for i in range(N-1):
    if S[i]=='A' and S[i+1]=='C':
        m += [i]
#print(m)
for i in range(Q):
    l,r=I()
    li=bisect.bisect_left(m,l-1)
    ri=bisect.bisect_left(m,r-1)
    #print(i,li,ri)
    print(ri-li-1 if m[ri-1]==r else ri-li)

Submission status: Runtime Error

Solution code:
a = list(map(int,input().split()))
N = a[0]
Q = a[1]
numofac = [0 for i in range(N)]
S = input()
appear = 0
index = 1
while index < N:
    if S[index-1] == "A" and S[index] == "C":
        appear += 1
        numofac[index] = appear
        numofac[index+1] = appear
        index += 2
    else:
        numofac[index] = appear
        index += 1
for i in range(Q):
    q = list(map(int,input().split()))
    print(str(numofac[q[1]-1] - numofac[q[0]-1]))

Submission status: Runtime Error

Solution code:
n, q = map(int, input().split())
s = input()

starts = [0 for k in range(q)]
ends = [0 for k in range(q)]
for i in range(q):
    starts[i], ends[i] = map(int, input().split())

answers = [0 for k in range(q)]
for i in range(q):
    tmpStr = s[starts[i]-1:ends[i]]
    tmpAns = 0
    for j in range(len(tmpStr)):
        if(tmpStr[j]=="A"):
            flag = 1
        elif(flag==1 and tmpStr[j]=="C"):
            tmpAns += 1
            flag = 0
        else:
            flag = 0
    answers[i] = tmpAns

for i in range(q):
    print(answers[i])

Submission status: Runtime Error

Solution code:
n, q = map(int,input().split())
s = input()

l = [0] * q
r = [0] * q

for i in range(0,q):
    l[i], r[i] = map(int,input().split())
    l[i] = l[i] -1
    r[i] = r[i] -1

s_list = list(s)

ac_list = [0] * (n-1)

for i in range(0,n-1):
    if s_list[i] == "A" and s_list[i+1] == "C":
        ac_list[i] = 1

for i in range(0,q):