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: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3
import sys


def solve(N: int, Q: int, S: str, l: "List[int]", r: "List[int]"):
    isAC = [False] * N
    for i in range(N-1):
        if S[i] == 'A' and S[i+1] == 'C':
            isAC[i] = True

    for i in range(Q):
        res = 0
        for j in range(l[i]-1,r[i]-1):
            if isAC[j]:
                res += 1
        print(res)


# Generated by 1.1.4 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
def main():
    def iterate_tokens():
        for line in sys.stdin:
            for word in line.split():
                yield word
    tokens = iterate_tokens()
    N = int(next(tokens))  # type: int
    Q = int(next(tokens))  # type: int
    S = next(tokens)  # type: str
    l = [int()] * (Q)  # type: "List[int]" 
    r = [int()] * (Q)  # type: "List[int]" 
    for i in range(Q):
        l[i] = int(next(tokens))
        r[i] = int(next(tokens))
    solve(N, Q, S, l, r)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
N,Q = (int(i) for i in input().split())
S = input()

answers = []
for i in range(Q):
    left,right = (int(i) for i in input().split())
    answers.append(S[left-1:right].count("AC"))

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

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int,input().split())
S = input()
for i in range(Q):
  l,r = map(int,input().split())
  chk = S[l-1:r]
  print(chk.count("AC"))

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int,input().split())
S = input()
cnt = 0
for i in range(Q):
  l,r = map(int,input().split())
  chk = S[l-1:r]
  print(chk.count("AC"))

Submission status: Time Limit Exceeded

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


for i in range(Q):
  count = 0
  l_i = int(l_and_r[i][0])
  r_i = int(l_and_r[i][1])
  while l_i < r_i:
    if S[l_i-1] == "A":
      if S[l_i] == "C":
        count += 1
        l_i += 2
    else:
      l_i += 1
  print(count)

Submission status: Time Limit Exceeded

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


for i in range(Q):
  count = 0
  l_i = int(l_and_r[i][0])
  r_i = int(l_and_r[i][1])
  for j in range(l_i, r_i):
    if S[j-1] == "A":
      if S[j] == "C":
        count += 1
  print(count)

Submission status: Time Limit Exceeded

Solution code:
N, Q = [int(nq) for nq in input().split()]
S = input()
AC = []
for s in range(len(S)-2):
    if S[s:s+2] == "AC":
        AC.append(s+1)
for q in range(Q):
    l, r = [int(lr) for lr in input().split()]
    ans = 0
    for ac in range(len(AC)):
        if l <= AC[ac] <= r-1:
            ans += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
ruisekiwa = [0] * (N + 1)
lists = []
S = str(input())
for t in range(1, len(S)):
    if S[t - 1] == "A" and S[t] == "C":
        for k in range(t, len(ruisekiwa)):
            ruisekiwa[k] += 1
for i in range(Q):
    l, r = map(int, input().split())
    ans = ruisekiwa[r - 1] - ruisekiwa[l - 1]
    lists.append(ans)
for m in range(len(lists)):
    print(lists[m])

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = str(input())
lists = []
for i in range(Q):
    l,r = map(int,input().split())
    m = S[l-1:r]
    lists.append(m.count("AC"))
for i in range(len(lists)):
    print(lists[i])

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
ruisekiwa = [0] * (N + 1)
lists = []
S = str(input())
for t in range(1, len(S)):
    if S[t - 1] == "A" and S[t] == "C":
        for k in range(t, len(ruisekiwa)):
            ruisekiwa[k] += 1
for i in range(Q):
    l, r = map(int, input().split())
    ans = ruisekiwa[r - 1] - ruisekiwa[l - 1]
    lists.append(ans)
for m in range(len(lists)):
    print(lists[m])

Submission status: Time Limit Exceeded

Solution code:
N, Q = [int(nq) for nq in input().split()]
S = input()
for q in range(Q):
    l, r = [int(lr) for lr in input().split()]
    ans = S[l-1:r].count("AC")
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, Q = [int(nq) for nq in input().split()]
S = input()
ANS = []
for q in range(Q):
    l, r = [int(lr) for lr in input().split()]
    ans = S[l-1:r].count("AC")
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, Q = [int(nq) for nq in input().split()]
S = input()
ANS = []
for q in range(Q):
    l, r = [int(lr) for lr in input().split()]
    ans = S[l-1:r].count("AC")
    ANS.append(ans)
    print(ANS[q])

Submission status: Time Limit Exceeded

Solution code:
N, Q = [int(nq) for nq in input().split()]
S = input()
ANS = []
for q in range(Q):
    l, r = [int(lr) for lr in input().split()]
    ans = S[l-1:r].count("AC")
    ANS.append(ans)
for q in range(Q):
    print(ANS[q])

Submission status: Time Limit Exceeded

Solution code:
N, Q = [int(nq) for nq in input().split()]
S = input()
ANS = []
for q in range(Q):
    l, r = [int(lr) for lr in input().split()]
    ans = 0
    for n in range(l-1, r-1):
        if S[n:n+2] == "AC":
            ans += 1
    ANS.append(ans)
for q in range(Q):
    print(ANS[q])

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
lr = []
for _ in range(0, Q):
    tmp_lr = list(map(int, input().split()))
    lr.append(tmp_lr)

for tmp_lr in lr:
    print(S[tmp_lr[0]-1:tmp_lr[1]].count("AC"))

Submission status: Time Limit Exceeded

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

lst = []
for _ in range(q):
    l, r = map(int, input().split())
    lst.append(s.count('AC', l-1, r))

print(*lst, sep='\n')

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
lst = [list(map(int, input().split())) for _ in range(q)]

for l, r in lst:
    print(s[l-1:r].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int, input().split())
s = input()
lst = [list(map(int, input().split())) for _ in range(q)]

for i in range(q):
    s_part = s[(lst[i][0]-1):lst[i][1]]
    print(s_part.count("AC"))

Submission status: Time Limit Exceeded

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

# print(N, Q)
# print(S)
# print(lr)

for i in lr:
  print(S[i[0]-1:i[1]].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
#input
N, Q = [int(i) for i in input().split(" ")]
st = input()
lis = []
for i in range(Q):
  lis.append([int(i) for i in input().split(" ")])
  
for i in range(Q):
  print(st[lis[i][0]-1:lis[i][1]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,q = list(map(int,input().split(' ')))
    s = input()
    s_tr =[]
    q_list = []
    for i in range(n-1):
        if len(s_tr) !=i:
            continue
        if s[i] == 'A' and s[i+1] =='C':
            s_tr.append(1)
            s_tr.append(1)
        else:
            s_tr.append(0)
    if len(s_tr) < n:
        s_tr.append(0)

 

    for i in range(q):
        l,r = list(map(int,input().split(' ')))
        if l ==1 or r ==n:
            print(sum(s_tr[l-1:r])//2)
        else:
            if s_tr[l-2] ==0 or s_tr[r] ==0:
                print(sum(s_tr[l-1:r])//2)
            else:
                print(sum(s_tr[l-1:r])//2 -1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def main():
    n,q = list(map(int,input().split(' ')))
    s = input()
    s_tr =[]
    q_list = []
    for i in range(n-1):
        if len(s_tr) !=i:
            continue
        if s[i] == 'A' and s[i+1] =='C':
            s_tr.append(1)
            s_tr.append(1)
        else:
            s_tr.append(0)
    if len(s_tr) < n:
        s_tr.append(0)

    for i in range(q):
        q_list.append(list(map(int,input().split(' '))))

    for l,r in q_list:
        if l ==1 or r ==n:
            print(sum(s_tr[l-1:r])//2)
        else:
            if s_tr[l-2] ==0 or s_tr[r] ==0:
                print(sum(s_tr[l-1:r])//2)
            else:
                print(sum(s_tr[l-1:r])//2 -1)

if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
n,m = map(int, input().strip().split())
s = input().strip()
d = {}
for i in range(n):
  for j in range(i,n):
    d[i+1,j+1] = s[i:j+1].count('AC')
    
for i in range(m):
  a,b = map(int, input().strip().split())
  print(d[a,b])

Submission status: Time Limit Exceeded

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

c_lst= []
for _ in range(q):
    l, r = map(int, input().split())
    l_r = s[l-1:r]
    count = l_r.count('AC')
    c_lst.append(count)

for i in c_lst:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

s_len, n = map(int, input().split())
s = input().rstrip()
for i in range(n):
  count = 0
  a, b = map(int, input().split())
  s_slice = s[a-1:b]  
  for i in range(len(s_slice)-1):
    if s_slice[i:2+i]=="AC":
      count += 1
  print(count)

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

s_len, n = map(int, input().split())
s = input().rstrip()
for i in range(n):
  a, b = map(int, input().split())
  s_slice = s[a-1:b]  
  print(sum([s_slice[i:2+i]=="AC" for i in range(len(s_slice)-1)]))

Submission status: Time Limit Exceeded

Solution code:
import sys
input = sys.stdin.readline

s_len, n = map(int, input().split())
s = input().rstrip()
for i in range(n):
  a, b = map(int, input().split())
  s_slice = s[a-1:b]  
  count = sum([s_slice[i:2+i]=="AC" for i in range(len(s_slice)-1)])
  print(count)

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int,input().split())
S = list(input())
T = [0] * N

for i in range(N-1):
    if S[i] == 'A':
        if S[i+1] == 'C':
            T[i] = 1
        else:
            T[i] = 0
    else:
        T[i] = 0

for j in range(Q):
    l, r = map(int,input().split())
    sum_AC = sum(T[l-1:r-1])
    print(sum_AC)

Submission status: Time Limit Exceeded

Solution code:
from sys import stdin

sN, sQ = stdin.readline().split()
N, Q = int(sN), int(sQ)
# print(N,Q)
S = stdin.readline().rstrip()
data = [stdin.readline().split() for itr in range(Q)]
# print(data)

for itr in range(Q):
    l = int(data[itr][0]) - 1 
    r = int(data[itr][1])
    s = S[l:r]
    # print(s)
    print(s.count('AC'))

Submission status: Time Limit Exceeded

Solution code:
NN, QQ = map(int, input().split())
SS = input('')
ll = [0]*(QQ)
rr = [0]*(QQ)
for ii in range(QQ):
    ll[ii], rr[ii] = map(int, input().split())
AC_indices = [0]*(NN)

for jj in range(NN-1):
    if SS[jj] == 'A' and SS[jj+1] == 'C':
        AC_indices[jj] += 1
        
for ii in range(QQ):
    print(sum(AC_indices[ll[ii]-1:rr[ii]-1]))

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int,input().split())
S = input()
L = []
R = []
for i in range(q):
    l, r = [int(i) for i in input().split()]
    L.append(l)
    R.append(r)
for i in range(q):
    ans = S[L[i]-1:R[i]].count("AC")

    print(ans)

Submission status: Time Limit Exceeded

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

lr = [list(map(int, input().split())) for i in range(Q)]

# AC探索
loc = []
for i in range(N-1):
    if S[i:i+2] == "AC":
        loc.append(i)

# lrの各セットにlocに含まれる位置がいくつあるのか調べる
for l,r in lr:
    cn = 0
    for i in range(l-1,r-1):
        if i in loc: cn += 1
    print(cn)

Submission status: Time Limit Exceeded

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

lr = [list(map(int, input().split())) for i in range(Q)]

for i in range(Q):
    print(S[lr[i][0]-1 : lr[i][1]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
def main(N, Q, S, lr_list):

    s_list = [0]
    for i in range(N):
        if i+1 < N and list(S)[i] == 'A' and list(S)[i+1] == 'C':
            s_list.append(s_list[i] + 1)
        else:
            s_list.append(s_list[i])

    res_list = []
    for lr in lr_list:
        l, r = lr
        l -= 1
        r -= 1
        res_list.append(s_list[r] - s_list[l])

    return res_list


if __name__ == "__main__":
    N, Q = list(map(int, input().split()))
    S = input()
    lr_list = []
    for _ in range(Q):
        lr_list.append(tuple(map(int, input().split())))
    res = main(N, Q, S, lr_list)
    for r in res:
        print(r)

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
lr = [list(map(int, input().split())) for _ in range(q)]

for i in range(q):
    left = lr[i][0] - 1
    right = lr[i][1]
    count = s[left:right].count('AC')
    print(count)

Submission status: Time Limit Exceeded

Solution code:
N , Q = map(int,input().split())
S = list(input())
count = 0

for i in range(Q):
    l,r = map(int,input().split())
    for j in range(r-l-1):
        if S[j] == 'A':
            if S[j+1] == 'C':
                count += 1
    print(count)
    count = 0

Submission status: Time Limit Exceeded

Solution code:
# C - GeT AC
# https://atcoder.jp/contests/abc122/tasks/abc122_c
import sys
n, q = list(map(int, sys.stdin.readline().split()))

s = sys.stdin.readline()

separators = [list(map(int, sys.stdin.readline().split())) for _ in range(q)]

# n = 8
# q = 3
# s = 'ACACTACG'
# separators = [[3, 7],[2, 3],[1, 8]]

# cnt = [0] * n


for i in range(0, len(separators)):
    print(s[separators[i][0]-1:separators[i][1]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
# C - GeT AC
# https://atcoder.jp/contests/abc122/tasks/abc122_c
import sys
n, q = list(map(int, sys.stdin.readline().split()))

s = input()

separators = [list(map(int, sys.stdin.readline().split())) for _ in range(q)]

# n = 8
# q = 3
# s = 'ACACTACG'
# separators = [[3, 7],[2, 3],[1, 8]]

cnt = []

for index in range(0, len(separators)):
    cnt.append(s[separators[index][0]-1:separators[index][1]].count('AC'))

for i in cnt:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
str = input()
for i in range(Q):
  l,r = map(int,input().split())
  substr = str[l-1:r]
  print(substr.count("AC"))

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
for i in range(Q):
  L, R = map(int, input().split())
  print(S[L-1:R].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
n,q=list(map(int,input().split()))
s=input()
l=list(range(q))
r=list(range(q))
for i in range(q):
    l[i],r[i]=input().split()
l=list(map(int,l))
r=list(map(int,r))

for i in range(q):
    print(s[l[i]-1:r[i]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
n,q=list(map(int,input().split()))
s=input()
l=list(range(q))
r=list(range(q))
for i in range(q):
    l[i],r[i]=list(map(int,input().split()))

for i in range(q):
    print(s[l[i]-1:r[i]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
n,q=list(map(int,input().split()))
s=input()
l=[]
r=[]
for i in range(q):
    la,ra=list(map(int,input().split()))
    l.append(la)
    r.append(ra)

for i in range(q):
    print(s[l[i]-1:r[i]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
for t in range(q):
	l, r = map(int, input().split())
	u = s[l-1:r]
	counter = 0
	for i in range(len(u)-1):
		if u[i] == "A" and u[i+1] == "C":
			counter += 1
	print(counter)

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
lr = [list(map(int, input().split())) for _ in range(q)]
ac_list = []
for i in range(1, len(s)):
    if s[i] == 'C' and s[i-1] == 'A':
        ac_list.append([i-1, i])

for i in range(q):
    # print(s[lr[i][0] - 1:lr[i][1]])
    # print(s[lr[i][0] - 1:lr[i][1]].count('AC'))
    count = 0
    for n in range(len(ac_list)):
        if ac_list[n][0] >= lr[i][0] - 1 and ac_list[n][1] <= lr[i][1] - 1:
            count += 1
    print(count)

Submission status: Time Limit Exceeded

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

for l, r in lr:
    print(S[l-1:r].count('AC'))

Submission status: Time Limit Exceeded

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

for i in range(Q):
    l, r = map(int, input().split())
    print(S[l-1:r].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
lr = [list(map(int, input().split())) for _ in range(q)]

for i in range(q):
    # print(s[lr[i][0] - 1:lr[i][1]])
    print(s[lr[i][0] - 1:lr[i][1]].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect

N, Q = map(int, input().split())
S = input()

ac_list = []
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        ac_list.append(i)

len_ac = len(ac_list)
for i in range(Q):
    l, r = map(int, input().split())
    left = l-1 if l-1 < len_ac else len_ac
    li_index = bisect.bisect_left(ac_list, l-1, 0, left)
    right = r-1 if r-1 < len_ac else len_ac
    ri_index = bisect.bisect_left(ac_list, r-1, 0, right)
    ans = len(ac_list[li_index:ri_index])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import bisect

N, Q = map(int, input().split())
S = input()

ac_list = []
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        ac_list.append(i)

for i in range(Q):
    l, r = map(int, input().split())
    li_index = bisect.bisect_left(ac_list, l-1)
    ri_index = bisect.bisect_left(ac_list, r-1)
    ans = len(ac_list[li_index:ri_index])
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
N,Q = map(int,input().split(' '))
S = input()

lr = []
for _ in range(Q):
    lr.append(list(map(int,input().split(' '))))

index_list = []
start = 0
for i in range(N):
    try:
        start += S[start:].index('AC')
        index_list.append(start)
        start += 2
    except ValueError:
        break

index_list = np.array(index_list)
for l,r in lr:
    print(np.sum((l-1 <= index_list) & (index_list <= r-2)))

Submission status: Time Limit Exceeded

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

for h in range(Q):
    count=0
    for k in range(L[h]-1,R[h]-1):
        if S[k]=='A' and S[k+1]=='C':
            count+=1
            continue
        continue
    print(count)

Submission status: Time Limit Exceeded

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

for h in range(Q):
    count=0
    word=[]
    for k in range(L[h]-1,R[h]):
        word.append(S[k])
        continue
    
    for g in range(len(word)-1):
        if word[g]=='A' and word[g+1]=='C':
            count+=1
        continue
    print(count)
    continue

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int,input().split())
s = input()
b = []
for t in range(n-1):
	if s[t] == "A" and s[t+1] == "C":
		b.append(t+1)
for u in range(q):
	l, r = map(int, input().split())
	C = []
	if l != r:
		C = [i for i in range(l, r)]
		D = set(b) & set(C)
		print(len(D))

Submission status: Time Limit Exceeded

Solution code:
n,q = (int(j) for j in input().split())
s = input()
x = [[0,0] for j in range(q)]
count = [0]*q
for j in range(q):
    x[j][0], x[j][1] = (int(k) for k in input().split())
for j in range(q):
    y = x[:]
    ss = s[y[j][0]-1:y[j][1]]
    ss = ss + 'B'
    for k in range(len(ss) - 1):
        if ss[k] == 'A' and ss[k+1] == 'C':
            count[j] = count[j] + 1
for j in range(q):
    print(count[j])

Submission status: Time Limit Exceeded

Solution code:
import sys
def input():
	return sys.stdin.readline()

N, Q = map(int, input().split())
S = input()
p = []
for _ in range(Q):
	p.append(tuple([int(_)-1 for _ in input().split()]))

def changeS(S):
	S_ = ['_'] * N
	for i in range(N-1):
		if S[i] == 'A' and S[i+1] == 'C':
			S_[i] = 'A'
	return S_

def cntdivS(S_, pi):
	return S_[pi[0]:pi[1]].count('A')

S_ = changeS(S)

for pi in p:
	print(cntdivS(S_, pi))

Submission status: Time Limit Exceeded

Solution code:
def main():
  N, Q = [int(x) for x in input().split()]
  S = input()
  counts = []
  for i in range(1, len(S) + 1):
    counts.append(S[:i].count('AC'))
  for i in range(Q):
    l, r = [int(x) for x in input().split()]
    print(counts[r - 1] - counts[l - 1])


if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:

def main():
  N, Q = [int(x) for x in input().split()]
  S = input()
  for i in range(Q):
    l, r = [int(x) for x in input().split()]
    sub = S[l - 1:r]
    print(sub.count('AC'))


if __name__ == "__main__":
  main()

Submission status: Time Limit Exceeded

Solution code:
import re
N,Q = map(int,input().split(' '))
S = input()

lr = []
for _ in range(Q):
    lr.append(list(map(int,input().split(' '))))

pattern = '(AC)'

for l,r in lr:
    match = re.findall(pattern, S[l-1:r])
    print(len(match))

Submission status: Time Limit Exceeded

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

t = S.replace('AC', 'aC')[:-1]

for i in range(Q):
    l, r = map(int, input().split())
    print(t[l-1:r-1].count('a'))

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
l = [list(map(int, input().split())) for _ in range(Q)]
for i in range(Q):
	print(S[l[i][0] - 1: l[i][1]].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
for i in range(Q):
	l, r = map(int, input().split())
	print(S[l - 1: r].count("AC"))

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

def main():
    n, q = map(int, input().split())
    s = input()
    slist = list(s)
    point = []
    for i in range(q):
        c = 0
        l, r = map(int, input().split())
        for j in range(l-1,r-1):
            if slist[j] == 'A' and slist[j+1] == 'C':
                c += 1
            else:
                pass
        point.append(c)

    for k in range(len(point)):
        print(point[k])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
#coding:utf-8

def main():
    n, q = map(int, input().split())
    s = input()
    slist = list(s)
    point = []
    for i in range(q):
        c = 0
        l, r = map(int, input().split())
        for j in range(l-1,r-1):
            if slist[j] == 'A':
                if slist[j+1] == 'C':
                    c += 1
                else:
                    pass
            else:
                pass
        point.append(c)

    for k in range(len(point)):
        print(point[k])


if __name__ == "__main__":
    main()

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
s = input()
a = []
for i in range(len(s)-1):
  if s[i] is 'A' and s[i+1] is'C':
    a.append(i)
for _ in range(q):
  li,ri = map(int,input().split())
  if len(a) is 0:
    print(0)
    continue
  print(len([aa for aa in a if li-1<=aa<ri-1]))

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
s = input()
q_con = []
for i in range(q) :
    q_con.append(list(map(int,input().split())))

count = [0 for i in range(q)]

list_2 = [i for i in range(q)]
list_3 = [i for i in range(q)]
for i in range(q) :

    a = q_con[i][0] - 1
    b = q_con[i][1] 
    list_2[i] = s[:][a:b]
    list_3[i] = list_2[:][i].replace("AC","")
    p = (len(list_2[i])-len(list_3[i]))/2
    print("{}".format(int(p)))

Submission status: Time Limit Exceeded

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

q_con = []
for i in range(q) :
    q_con.append(list(map(int,input().split())))



count = [0 for i in range(q)]
for i in range(q) :
    a = q_con[i][0] + 1
    b = q_con[i][1] + 1
    for j in range(a,b) :
        if s[j] == "A" and s[j+1] == "C" :
            count[i] += 1
        else : pass 

for i in count :
    print(i)

Submission status: Time Limit Exceeded

Solution code:
from math import ceil


def main(N, Q, S, lr_list):
    res = []
    for lr in lr_list:
        l, r = lr
        res.append(S[l-1:r].count("AC"))
    return res


if __name__ == "__main__":
    N, Q = list(map(int, input().split()))
    S = input()
    lr_list = []
    for _ in range(Q):
        lr_list.append(tuple(map(int, input().split())))
    res = main(N, Q, S, lr_list)
    for r in res:
        print(r)

Submission status: Time Limit Exceeded

Solution code:
#!/usr/bin/env python3


def main():
    n, q = map(int, input().split())
    s = input()
    l = []
    r = []
    for _ in range(q):
        li, ri = map(int, input().split())
        l.append(li-1)
        r.append(ri-1)

    # x[i] = 0文字目からi文字目までのACの出現回数の総和
    y = [i for i, v in enumerate(s) if i < n - 1 and s[i] + s[i+1] == 'AC']
    x = []
    for i in range(n):
        before = 0 if len(x) == 0 else x[-1]
        if i in y:
            x.append(before + 1)
        else:
            x.append(before)
    for i in range(q):
        li = l[i]
        ri = r[i]
        lsum = 0 if li == 0 else x[li-1]
        rsum = x[ri-1]
        print(rsum - lsum)


if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import re

rexp = re.compile("AC")

N, Q = map(int, input().split())
S = input()
ruiseki_S = [""]
for i in range(N):
    ruiseki_S.append(ruiseki_S[i] + S[i])
for i in range(Q):
    l, r = map(int, input().split())
    a = ruiseki_S[r].replace(ruiseki_S[l - 1], "", 1)
    print(len(rexp.findall(a)))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
data = []
for i in range(Q):
    l, r = map(int, input().split())
    data.append((l, r))
#print(data)
for (l, r) in data:
    #print(S[l-1:r])
    print(S[l-1:r].count('AC'))

Submission status: Time Limit Exceeded

Solution code:

N, Q = map(int, input().split())
S = input()
LRs = [list(map(int, input().split())) for i in range(Q)]

counts = []
for i in range(len(S)-1):
    if S[i] == 'A' and S[i+1] == 'C':
        counts.append(1)
    else:
        counts.append(0)
counts.append(0)

for LR in LRs:
    start = LR[0] - 1
    end = LR[1] 
    out = 0
    for i in range(start, end-1):
        out += counts[i]
    print(out)

Submission status: Time Limit Exceeded

Solution code:

N, Q = map(int, input().split())
S = input()
LRs = [list(map(int, input().split())) for i in range(Q)]

counts = []
for i in range(len(S)-1):
    if S[i] == 'A' and S[i+1] == 'C':
        counts.append(1)
    else:
        counts.append(0)
counts.append(0)

for LR in LRs:
    start = LR[0] - 1
    end = LR[1] 
#     print(S[start:end])
#     print(counts[start:(end-1)])
    print(sum(counts[start:(end-1)]))

Submission status: Time Limit Exceeded

Solution code:

N, Q = map(int, input().split())
S = input()
LRs = [list(map(int, input().split())) for i in range(Q)]

for LR in LRs:
    s = S[LR[0]-1 : LR[1]]
    count = 0
    for i in range(len(s)-1):
        if s[i] == 'A' and s[i+1] == 'C':
            count += 1
    print(count)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    ans = 0
    substr = S[L[i] - 1:R[i]]
    while substr:
        i = substr[:-1].find('A')
        if i < 0:  # not found
            break
        elif substr[i + 1] == 'C':  # found AC
            ans += 1
            substr = substr[i + 2:]
        else:  # found A without C
            substr = substr[i + 1:]
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    w = S[List[i][0]-1:List[i][1]]
    cnt = w.count("AC")
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
N,Q=[int(i) for i in input().split()]
S=input()
for _ in range(Q):
  lr=[int(i) for i in input().split()]
  cnt=0
  for i in range(lr[0]-1,lr[1]-1):
    if S[i:i+2]=="AC": cnt+=1
  print(cnt)

Submission status: Time Limit Exceeded

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

for i in range(q):
    l,r = map(int,input().split())
    ans.append(s[l-1:r].count('AC'))

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

Submission status: Time Limit Exceeded

Solution code:
l, n = [int(x) for x in input().split()]
s = input()
for i in range(n):
  q = [int(x) for x in input().split()]
  ans = 0
  for j in range(q[0]-1, q[1]-1):
    if s[j:j+2] == 'AC':
      ans += 1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
N, Q = (int(i) for i in input().split())
S = input()
l = []
r = []
for i in range(Q):
    a, b = (int(i) for i in input().split())
    l.append(a)
    r.append(b)

#print(N, Q, l, r)
#print(S)

for i in range(Q):
    pre_str = S[l[i]-1:r[i]]
    print(pre_str.count('AC'))

Submission status: Time Limit Exceeded

Solution code:
num = list(map(int,input().split()))
string = input()
s_key=[]
#count=[]

for i in range(num[1]):
    s_key.append(list(map(int,input().split())))
    '''
for i in range(num[0]):
    if i==0:
        count.append(0)
    else:
        if (string[i-1]=='A' and string [i]=='C'):
            count.append(count[i-1]+1)
        else:
            count.append(count[i-1])
'''
for k in range(num[1]):
   # result=count[s_key[k][1]-1]-count[s_key[k][0]-1]
    #print(result)
   newone=string[s_key[k][0]-1:s_key[k][1]]
   print(newone.count('AC'))