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:
import re
N, Q = map(int, input().split())
S = input()
indices = [m.start(0) for m in re.finditer("AC", S)]

for _ in range(Q):
    li, ri = map(int, input().split())
    print(len([x for x in indices if li - 1 <= x and x < ri - 1]))

Submission status: Time Limit Exceeded

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

for _ in range(Q):
	l, r = [int(x) for x in input().split()]
	print(sum(AC_idx[l:r-2]))

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]"):
    ans = []
    carray = [0] * N
    for i in range(1,N):
        if(S[i] == 'C' and S[i-1] == 'A'):
            carray[i:] = map(lambda x:x+1, carray[i:])
    
    # print(carray)
    for i in range(Q):
        # print(carray[l[i]-1:r[i]])
        ans.append(carray[r[i]-1]-carray[l[i]-1])

    for i in range(Q):
        print(ans[i])
    return


# Generated by 1.1.3 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:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

N, Q = LI()
S = input()
"""
ACのインデックスをすべて記録しておき、始点、終点で区切って出す
少ない方を取ったほうがいいかな
"""
ACs = []
for sidx in range(0, len(S)-1):
    if S[sidx] == 'A':
        if S[sidx+1] == 'C':
            ACs.append(sidx)

#  print(ACs)

LR = []
for i in range(Q):
    LR.append(LI())

#  N, Q = 10**5, 10**5
#  S = 'ACTCTTCGTG'*10**4
#  LR = []
#  for i in range(Q):
#      LR.append([3,9000])

#  for l, r in LR:
#      tmp = S[l-1:r]
#      print(tmp.count('AC'))

result = []
#  print(ACs)
for l, r in LR:
    start = bisect.bisect_left(ACs, l-1)
    end = bisect.bisect_left(ACs, r-1)
    #  print('s,e', start, end)
    print(len(ACs[start:end]))

Submission status: Time Limit Exceeded

Solution code:
n,q = list(map(int,input().split()))
s = list(input())
num = list()
for i in range(0,q):
    num.append(list(map(int,input().split())))
check = list()
for i in range(0,n-1):
    if s[i] == "A" and s[i+1] == "C":
        check.append(i+1)
count = list()
for i in range(0,q):
    for k in check:
        if num[i][0] <= k and num[i][1] > k:
            count.append(k)
    print(len(count))
    count = list()

Submission status: Time Limit Exceeded

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

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

for i in range(Q):
	c=0
	print(sum(l[i]-1<=j and j<r[i]-1 for j in acs))

Submission status: Time Limit Exceeded

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

for i in range(q):
  a,b=list(map(int, input().split()))
  counter=0
 
  for j in range(len(S[a-1:b])-1):
    if S[a-1:b][j] == "A":
      if S[a-1:b][j+1] == "C":
        counter +=1

  print(counter)

Submission status: Time Limit Exceeded

Solution code:
import re
N,Q=[int(e) for e in input().split(' ')]
S=input()

qs=[]
for i in range(Q):
    qs.append([int(e) for e in input().split(' ')])

for q in qs:
    substr=S[q[0]-1:q[1]]
    print(len(re.findall('AC',substr)))

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
b = input()
a = [[int(x) for x in input().split()] for _ in range(q)]
for i in a:
    ans = 0
    s = b[i[0]-1:i[1]]
    for j in range(len(s)-1):
        if s[j] == "A" and s[j+1] == "C":
            ans += 1
    print(ans)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    s = lr[i][0] - 1
    l = lr[i][1]
    tempS = S[s:l]
    print(tempS.count('AC'))

Submission status: Time Limit Exceeded

Solution code:
def counter(n, m):
	c = 0
	for o in range(n-1, m):
		if S[o] == "A":
			if o+1 <= m-1:
				if S[o+1] == "C":
					c = c + 1
	return c
N, Q = map(int, input().split())
A = input()
S = [str(c) for c in A]
x = []
y = []
for i in range(Q):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1)
for i in range(Q):
	print(counter(x[i], y[i]))

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
ac_list = [0]*len(s)
i = 0
'''while True:
    ac_index = s[i:].find("AC")
    if ac_index == -1:
        break
    else:
        ac_list[ac_index+i] = 1
        i = ac_index + i + 2'''

'''for i in range(len(s)-1):
    if s[i] == 'A' and s[i+1] == 'C':
        ac_list[i] = 1'''

for _ in range(q):
    lr = tuple(map(int, input().split()))
    print(s.count("AC", lr[0]-1, lr[1]))

Submission status: Time Limit Exceeded

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

L=[]
for i in range(Q):
	L.append(map(int, raw_input().split()))

for i in L:
	#print S[i[0]-1:i[1]]
	print S[i[0]-1:i[1]].count("AC")

Submission status: Time Limit Exceeded

Solution code:
def solv(s, l, r):
    s = s[l-1:r]
    return s.count('AC')


if __name__ == '__main__':

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

    for _ in range(q):
        l, r = map(int, input().split())
        ans = solv(s, l, r)
        print(ans)

Submission status: Time Limit Exceeded

Solution code:
import numpy as np
#
#
# def count(s):
#     num = 0
#     check = False
#     for i in s:
#         if i == 'A' and not(check):
#             check = True
#         elif i == 'C' and check:
#             num += 1
#             check = False
#         else:
#             check = False
#     return num


_, q = map(int, input().split())
s = input()
index = np.array([s1 == 'A' and s2 == 'C' for i, (s1, s2) in enumerate(zip(s[:-1], s[1:]))])
# s = ''.join([i if i == 'A' or i == 'C' else ' ' for i in s])
inds = np.array([list(map(int, input().split())) for _ in range(q)])
# ss = s[ind[0]-1:ind[1]]
# print(count(ss))
tmp = [str(np.count_nonzero(index[ind[0]-1:ind[1]-1])) for ind in inds]
print('\n'.join(tmp))

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = input()
tmp = ''
kesu = ''
ans = 0
LR = []
for i in range(Q):
    array = list(map(int, input().strip().split(' ')))
    LR.append(array)
    
for i in range(Q): # 問iを解く
    ans = 0
    tmp = S[LR[i][0]-1:LR[i][1]]
    kesu = tmp.replace('AC', '')
    ans = (LR[i][1]-LR[i][0]+1-len(kesu))/2
    print(int(ans))

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
line = list(input())
AC_locate = []
count_s = []
for i in range(N-1):
    if line[i] == 'A' and line[i+1] == 'C':
        AC_locate.append(int(i))
for i in range(Q):
    l,r = map(int,input().split())
    count = 0
    for locate in AC_locate:
        if locate > r-2:
            break
        if locate > l-2:
            count += 1
    # for i in range(l-1,r):
    #     if line[i] == 'A' and line[i+1] == 'C':
    #         count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
target_str = "AC"

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

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

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
def findAC(s):
    retSum = 0
    for i in range(len(s)-1):
        if s[i] == "A" and s[i+1] == "C":
            retSum+=1
    return retSum

n,q = map(int,input().split())
s = input()
lr = []
for i in range(q):
    lr.append([int(i) for i in input().split()])
for i in range(q):
    l = lr[i][0]
    r = lr[i][1]
    s_q = s[l-1:r]
    print(findAC(s_q))

Submission status: Time Limit Exceeded

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


for q in range(Q):
  l,r = map(int, input().split())
  s_q = s[l-1:r]
  cnt = 0
  
  for i in range(len(s_q)-1):
    if s_q[i:i+2] == 'AC':
      cnt += 1
  
  print(cnt)

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)

list = []
flg = 0
for s in S:
    if s == "A" and flg == 0:
        flg = 1
    elif s == "C" and flg == 1:
        list.append(1)
        list.append(2)
        flg = 0
    elif flg == 1:
        list.append(0)
        list.append(0)
    else:
        list.append(0)

for i in range(Q):
    S = list[L[i] - 1:]
    S = S[:R[i] - L[i] + 1]
    if S[0] == 2:
        S = S[1:]
    if S[-1] == 1:
        S = S[:-2]
    print(int(sum(S)/3))

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
s = input()
count = []
tmp = 0
L = []
R = []
for i in range(Q):
    a = list(map(int, input().split()))
    L.append(a[0])
    R.append(a[1])
for r in range(Q):
    tmp = 0
    for b in range(L[r]-1, R[r]-1):
        if s[b] == 'A' and s[b+1] == 'C':
            tmp += 1
    count.append(tmp)
for k in count:
    print(k)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,q=[int(i) for i in input().split()]
s=input()
place=tuple(tuple(int(i) for i in input().split()) for _ in range(q))
for i in place:
    print(s.count("AC",i[0]-1,i[1]))

Submission status: Time Limit Exceeded

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

def counter(S):
  count1 = 0
  for i in range(len(S)-1):
    if((S[i] == 'A')and(S[i+1] == 'C')):
      count1 += 1
  return count1


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

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split())
S=input()
lr=[]
n=0
for i in range(Q):
	l,r=map(int,input().split())
	s=S[l-1:r]
	for j in range(r-l):
		if s[j]=="A" and s[j+1]=="C":
			n=n+1
	print(n)
	n=0

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = list(input())
s = []
for i in range(Q):
    l,r = map(int,input().split())
    c = 0
    #print(Q,i)
    s.append(0)
    #print(s)
    for j in range(l-1,r):
        #print(i,j)
        if c == 0 and S[j] == "A":
            c = 1
            #print("a")
        elif c == 1 and S[j] == "C":
            c = 0
            s[i] = s[i]+ 1
            #print("b")
    #print(i)
for i in range(Q):
    print(s[i])

Submission status: Time Limit Exceeded

Solution code:
import sys

def parse(inp):
    N,Q=map(int,next(inp).split())
    S=list(next(inp))
    
    lr=[[] for _ in range(Q)]
    
    for i in range(Q):
        lr[i]=list(map(int,next(inp).split()))
        
    return N,Q,S,lr

def whereAC(S):
    ACpoint=[]
    for i in range(len(S)-1):
        if(S[i]=='A' and S[i+1]=='C'):
            ACpoint.append(i)
            
    return ACpoint
  
def main(inp):
    N,Q,S,lr=inp
       
    ACpoint=whereAC(S)   
    for i in range(Q):
        l,r=lr[i]
        ans=0
        for j in range(len(ACpoint)):
            if(ACpoint[j] >= l-1 and ACpoint[j] <= r-2):
                ans+=1
        print(ans)
        
main(parse(sys.stdin))

Submission status: Time Limit Exceeded

Solution code:
#8 3
#ACACTACG
#=> ACACxACx
#3 7
#2 3
#1 8

N,Q=map(int, raw_input().split())
S=list(raw_input())

p_list=[]
cnt=0

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

#print p_list

for i in range(Q):
    q_pos = map(int,raw_input().split())
#    print S[q_pos[0]-1:q_pos[1]]
    cnt = 0
    for p in p_list:
        if (p >= (q_pos[0]-1)) and (p <(q_pos[1]-1)):
#            print q_pos[0]-1, p, q_pos[1]-1
            cnt += 1
    print cnt

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]"):
    ans = []
    carray = [0] * N
    for i in range(1,N):
        if(S[i] == 'C' and S[i-1] == 'A'):
            carray[i:] = map(lambda x:x+1, carray[i:])
        else:
            continue
    
    # print(carray)
    for i in range(Q):
        # print(carray[l[i]-1:r[i]])
        ans.append(carray[r[i]-1]-carray[l[i]-1])

    for i in range(Q):
        print(ans[i])
    return


# Generated by 1.1.3 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 = map(int, input().split())
S = input()
list = []
for i in range(Q):
    start,end = map(int, input().split())
    mozi = S[start-1:end]
    print(mozi.count('AC'))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

A = list(map(int, input().split(" ")))
str = input()
count = 0

for i in range(A[1]):
    Q = list(map(int, input().split(" ")))
    S = str[Q[0]-1:Q[1]]
    for j in range(len(S)):
        if S[j:j+1] == 'A' :
            if S[j+1:j+2] == 'C' :
                count = count+1
                J = j+1
    print(count)
    count = 0

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
l=[]; r=[]
for _ in range(Q):
    l1, r1 = map(int, input().split())
    l.append(l1); r.append(r1)
import re
regex = re.compile(r"AC")
for i in range(Q):
    mo = regex.findall(S[l[i]-1:r[i]])
    print(len(mo))

Submission status: Time Limit Exceeded

Solution code:


N,Q=input().split(" ")
N=int(N)
Q=int(Q)

S=input()

L=[]
R=[]
D=0
for i in range(Q):
	l,r=input().split(" ")
	L.append(int(l))
	R.append(int(r))

D=sum(R)-sum(L)#距離の総和 交換法則よりこの方法で求められる



##Nが十分小さい時 O(N+Q)になる
#十分大きいというより, かぶることがあるときかな???

if(D>N):
	count=[0 for i in range(N)]
	last="X"
	for i in range(N):
		if(S[i]=="C"):
			if(last=="A"):
				count[i]+=1
		last=S[i]


	for i in range(Q):
		c=sum(count[L[i]-1:R[i]])
		if(L[i]-2>=0):
			if(S[L[i]-1]=="C" and S[L[i]-2]=="A"):
				c-=1
		print(c)
else:
#if(1):
	for i in range(Q):
		s=S[L[i]-1:R[i]]
		c=0
		last="X"
		for j in range(len(s)):
			if(s[j]=="C" and last=="A"):
				c+=1
			last=s[j]
		print(c)

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

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

while i < q:
  l, r = map(int, input().split())
  print(s[l - 1: r].count("AC"))
  i += 1

Submission status: Time Limit Exceeded

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

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

print('\n'.join(map(str, ans)))

Submission status: Time Limit Exceeded

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

w =[0]*q
w_n =[0]*q

for i in range(q):
    w[i] = s[(l[i][0]-1):(l[i][1])]
    

for i in range(q):
    w[i].strip('T')
    w[i].strip('G')
    
c=0

for i in w:
    w_n[c]=i.count('AC')
    c+=1
    
for i in w_n:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
def countAC(s):
    index = []
    for i in range(len(s)):
        if i == len(s)-1:
            break
        if s[i] == 'A':
            if s[i+1] == 'C':
                index.append(1)
                continue
        index.append(0)
    return index

N,Q = (int(_) for _ in input().split())
S = input()
SIndex = countAC(S)
out = 0
for i in range(Q):
    l,r = (int(_) for _ in input().split())
    s = SIndex[l-1:r-1]
    for x in s:
        out += x
    print(out)
    out = 0

Submission status: Time Limit Exceeded

Solution code:
import bisect
import numpy as np
n, q = map(int, input().split())
s = input()
ac = 'AC'
args = []
for i in range(len(s) - 1):
    if s[i:i+2] == 'AC':
        args.append(i)
args = np.array(args)
for j in range(q):
    l, r = map(int, input().split())
    i_l = bisect.bisect_left(args, l-1)
    i_r = bisect.bisect_right(args, r-2)
    print(len(args[i_l:i_r]))

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
s = input()
lr = [[int(i) for i in input().split()]for _ in range(q)]
count,ans,f = 0,0,0
for j in lr:
  ans = 0
  f = 0
  for i in s[j[0]-1:j[1]]:
    
    if i =='A':
      f = 1
    elif i =='C' and f==1:
      ans+=1
      f =0
    else:
      f = 0
  print(ans)

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())
  j = l-1
  AC = 0
  while j < r-1:
    if s[j] == "A" and s[j+1] == "C":
      AC += 1
      j += 1
    j += 1
  print(AC)

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
for x in range(Q):
    a, b = map(int, input().split())
    s = S[a-1:b]

    a = ''
    ret = 0
    for b in s:
        if a == 'A' and b == 'C':
            ret += 1
        a = b
    print(ret)

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int,input().split())
S = input()
LRlist = []
for i in range(Q):
    LRlist.append(list(map(int,input().split())))
indexlist = []
for i in range(len(S)-1):
    if S[i] == 'A':
        if S[i+1] == 'C':
            indexlist.append(i)
for LR in LRlist:
    L = LR[0]
    R = LR[1]
    answer = 0
    for i in indexlist:
        if L - 1 <= i <= R - 2:
            answer += 1
    print(answer)

Submission status: Time Limit Exceeded

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

for lr in lr_list:
    tmp = (S[lr[0] - 1:lr[1]]).replace("G","").replace("T","")
    print(tmp.count("AC"))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
import sys
import collections

def main():
	N, Q = map(int, input().split(" "))
	S = input()
	S = list(S)
	
	Qs = []
	for _ in range(Q):
		l, r = map(int, input().split(" "))
		Qs.append((l, r))
	
	ans = []
	for i, val in enumerate(Qs):
		l, r = val
		tmp = S[l-1:r]
		ans_t = 0
		
		for j, val in enumerate(tmp):
			if val == "A":
				if j+1 < len(tmp) and tmp[j+1] == "C":
					ans_t += 1
		ans.append(ans_t)
	
	
	for i in ans:
		print(i)
	
if __name__ == "__main__":
	main()

Submission status: Time Limit Exceeded

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

l_list = []
r_list = []

ans = []
for x in range(Q):
    l, r = map(int, input().split())
    sub_str = S[l-1:r]
    ac_count = 0
    
    for i in range(len(sub_str) - 1):
        if sub_str[i:i+2] == 'AC':
            ac_count += 1
    
    ans.append(ac_count)
    
for a in ans:
    print(a)

Submission status: Time Limit Exceeded

Solution code:
import re

pattern = "AC"
N, Q = map(int, input().split(" "))
S = input()
for i in range(Q) :
    l, r = map(int, input().split(" "))
    results = re.findall(pattern, S[l-1:r], re.S)
    print(len(results))

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()
count=[0]*(N-1)
for i in range(N-1):
    if S[i]=="A" and S[i+1]=="C":count[i]=1
for i in range(Q):
    l,r=map(int,input().split())
    print(sum(count[l-1:r-1]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int,input().split())
s = input()
l = [[int(i) for i in input().split()] for i in range(q)]
c = [0 for i in range(n)]
cc = 0
f = False
for i in range(n):
    t = s[0:i+1]
    c[i] = t.count("AC")
for i in range(q):
    print(c[l[i][1]-1]-c[l[i][0]-1])

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
s = list(str(input()))
k = [0 for i in range(n)]
for i in range(n-1):
    if s[i] == "A" and s[i+1] == "C":
        k[i+1] = 1
#print(k)
for i in range(q):
    a,b = map(int,input().split())
    a -= 1
    b -= 1
    #print(k[a:b+1])
    ans = sum(k[a:b+1])
    if k[a] == 1:
        ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:

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

a = [0]*Q

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

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

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
l_li=[]
r_li=[]
for i in range(q):
    l,r=map(int,input().split())
    l_li.append(l)
    r_li.append(r)
    
    
dp =[[0 for i in range(n+2)] for j in range(n+2)]

for i in range(n):
    for j in range(i,n):
        t=0
        if s[j]=="C" and j!=i:
            if s[j-1]=="A":
                t=1
        
        dp[i][j]=dp[i][j-1]+t
        
for i in range(q):
    print(dp[l_li[i]-1][r_li[i]-1])

Submission status: Time Limit Exceeded

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

AC_list = [0] * N

for i in range(Q):
    l, r = (int(_) for _ in input().split())
    partial_S = S[l-1: r]
    counter = 0
    
    print(partial_S.count("AC"))

Submission status: Time Limit Exceeded

Solution code:
import math, string, itertools, fractions, heapq, collections, re,  array, bisect, sys, random, time, copy, functools
sys.setrecursionlimit(10**7)
inf = 10 ** 20
eps = 1.0 / 10**10
mod = 10**9+7
dd = [(-1, 0), (0, 1), (1, 0), (0, -1)]
ddn = [(-1, 0), (-1, 1), (0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1)]
def LI(): return [int(x) for x in sys.stdin.readline().split()]
def LI_(): return [int(x)-1 for x in sys.stdin.readline().split()]
def LF(): return [float(x) for x in sys.stdin.readline().split()]
def LS(): return sys.stdin.readline().split()
def I(): return int(sys.stdin.readline())
def F(): return float(sys.stdin.readline())
def pf(s): return print(s, flush=True)

N, Q = LI()
S = input()
"""
ACのインデックスをすべて記録しておき、始点、終点で区切って出す
少ない方を取ったほうがいいかな
"""
ACs = []
for sidx in range(0, len(S)-1):
    if S[sidx]+S[sidx+1] == 'AC':
        ACs.append(sidx)

#  print(ACs)
#  LR = []
#  for i in range(Q):
#      LR.append(LI())
#  N, Q = 10**5, 10**5
#  S = 'ACTCTTCGTG'*10**4
#  LR = []
#  for i in range(Q):
#      LR.append([3,9000])

#  for l, r in LR:
#      tmp = S[l-1:r]
#      print(tmp.count('AC'))

result = []
#  print(ACs)
for _ in range(Q):
    l,r = LI()
    start = bisect.bisect_left(ACs, l-1)
    end = bisect.bisect_left(ACs, r-1)
    #  print('s,e', start, end)
    res = len(ACs[start:end])
    result.append(res)
for i in result:
    print(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())
  print(''.join(S[l-1:r]).count('AC'))

Submission status: Time Limit Exceeded

Solution code:
# -*- coding: utf-8 -*-
N,Q = map(int, input().split())
S = input()
fl = []
for n in range(N-1):
    if S[n:n+2] == "AC":
        fl.append(1)
    else:
        fl.append(0)

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

Submission status: Time Limit Exceeded

Solution code:
import re
N, Q = map(int, input().split())
S = input()
iter = re.finditer("AC", S)
indices_A = [m.start(0) for m in iter]
indices_C = [a+1 for a in indices_A]
total = S.count("AC")

dec_l_list = []
dec_l = 0
for i in range(N):
    if i in indices_C:
        dec_l-= 1
    dec_l_list.append(dec_l)

dec_r_list = []
dec_r = 0
for i in reversed(range(N)):
    if i in indices_A:
        dec_r-= 1
    dec_r_list.append(dec_r)
dec_r_list.reverse()

#print(dec_l_list)
#print(dec_r_list)

for i in range(Q):
    l, r = map(int, input().split())
    print(total + dec_l_list[l-1] + dec_r_list[r-1])

Submission status: Time Limit Exceeded

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

for i in range(Q):
    print(len((S[l[i]-1:r[i]]).split("AC"))-1)

Submission status: Time Limit Exceeded

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

for i in range(Q):
    l, r = (int(i) for i in input().split())
    charlist = char[(l - 1) : r]
    result = charlist.count("AC")
    print(result)

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
S = input()
lr = [[int(i) for i in input().split()] for i in range(Q)]
C = []
for i in range(N-1):
    if S[i] == "A":
        if S[i+1] == "C":
            C.append(i)
for i in lr:
    AC = 0
    for j in C:
        if j >= i[0]-1:
            if j < i[1]-1:
                AC += 1
            else:
                break
    print(AC)

Submission status: Time Limit Exceeded

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

li = []

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

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
lis = []
for i in range(len(S) - 1):
  if S[i] == "A" and S[i + 1] == "C":
    lis.append(i + 1)
for _ in range(Q):
  l, r = map(int, input().split())
  count = 0
  for item in lis:
    if l <= item < r:
      count += 1
  print(count)

Submission status: Time Limit Exceeded

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

for _ in range(Q):
    l, r = map(int, input().split())
    print(S.count("AC", l - 1, r))
    # QS.append((l, r))
#
# for l, r in QS:
#     # print(S[l - 1:r])
#     print(S.count("AC", l - 1, r))

Submission status: Time Limit Exceeded

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

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

for _ in range(Q):
  l, r = map(int, input().split())
  i = l - 1
  cnt = 0
  if S[i + 1] == "C":
    if S[i] == "A":
      cnt += 1
  while i < r - 1:
    if S[i + 1] == "C":
      if S[i] == "A":
        cnt += 1
      i += 2
    else:
      i += 3
  print(cnt)

Submission status: Time Limit Exceeded

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

for lr_v in lr:
    k = 0
    for i in range(lr_v[0] - 1, lr_v[1], 2):
        if s[i] == "A" and s[i + 1] == "C":
            k += 1

        if i + 2 < lr_v[1]:
            if s[i + 1] == "A" and s[i + 2] == "C":
                k += 1

    print(k)

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int,input().split())
s = str(input())
S = []
for c in s:
    S.append(c)

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

Submission status: Time Limit Exceeded

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

l_list = []
r_list = []

for q in range(Q):
  l,r = map(int, input().split())
  s_q = s[l-1:r]
  cnt = 0
  
  for i in range(len(s_q)-1):
    if s_q[i] == 'A' and s_q[i+1] == 'C':
      cnt += 1
  
  print(cnt)

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

q=[]
for i in range(Q):
    a,b=map(int,input().split())
    q.append([a,b])
kekka=[] 
#print(S)
for i in range(N-1):
    if S[i]=="A"and S[i+1]=="C":
        kekka.append(i+1)
#print(kekka)
c=len(kekka)
for i in range(Q):
    count=0
    for j in range(c):
        if q[i][0]<=kekka[j] and q[i][1]>kekka[j]:
            count+=1
            
    print(count)