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:
N,Q = map(int, input().split())
S = str(input())
L = [0] * len(S) #0はACのAでない
i = 0
while i < len(S)-1:
    if S[i] == "A":
        if S[i+1] == "C":
            L[i] = 1
            i += 1
        elif S[i+1] == "A":
            i += 1
        else:
            i += 2
    else:
        i += 1
for _ in range(Q):
    l, r = map(int, input().split())
    print(sum(L[l-1:r-1]))

Submission status: Time Limit Exceeded

Solution code:
N,Q = map(int, input().split())
S = str(input())
for _ 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()
l=[]
r=[]
sum=0
for i in range(q):
  L,R=map(int,input().split())
  l.append(L)
  r.append(R)
for i in range(q):
  for j in range(l[i]-1,r[i]-1):
    if s[j]=="A" and s[j+1]=="C":
      sum+=1
  print(sum)
  sum=0

Submission status: Time Limit Exceeded

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

for q in qs:
    print(sum(bi_gram[q[0] - 1: q[1] - 1]))

Submission status: Time Limit Exceeded

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

for q in qs:
    cnt = 0
    target = S[q[0]-1:q[1]]
    for i in range(q[1] - q[0]):
        if target[i:i+2] == "AC":
            cnt += 1
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
N, Q = map(int, input().split(" "))
S = input()
bi_gram = [S[k:k+2] for k in range(N - 1)]
qs = [list(map(int, input().split(" "))) for i in range(Q)]

for q in qs:
    print(reduce(lambda x, y: x + (1 if y == "AC" else 0), bi_gram[q[0]-1: q[1]-1], 0))

Submission status: Time Limit Exceeded

Solution code:
from functools import reduce
N, Q = map(int, input().split(" "))
S = input()
qs = [list(map(int, input().split(" "))) for i in range(Q)]

for q in qs:
    t = S[q[0]-1:q[1]]
    bi_gram = [t[k:k+2] for k in range(len(t) - 1)]
    print(reduce(lambda x, y: x + (1 if y == "AC" else 0), bi_gram, 0))

Submission status: Time Limit Exceeded

Solution code:
a=list(map(int,input().split()))
s=input()
b=[]
for i in range(a[1]):
  b.append(list(map(int,input().split())))

for i in range(a[1]):
  c=0
  m = s[b[i][0]-1:b[i][1]]
  for j in range(len(m)-1):
    if m[j:j+2] == "AC":
      c +=1
  print(c)

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = str(input())
LR = [list(map(int, input().split())) for i in range(Q)]
cnt = 0
for i in range(Q):
    STR = S[LR[i][0]-1:LR[i][1]]
    for i in range(len(STR)-1):
        if(STR[i:i+2] == 'AC'):
            cnt += 1
    print(cnt)
    cnt=0

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = str(input())
LR = [list(map(int, input().split())) for i in range(Q)]
for i in range(Q):
    STR = S[LR[i][0]-1:LR[i][1]]
    if('AC' in STR):
        print(STR.count('AC'))
    else:
        print('0')

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = str(input())
i = 1
while i <= Q:
    l, r = map(int, input().split())
    s = S[l-1:r]
    ans = s.count('AC')
    print (ans)
    i += 1

Submission status: Time Limit Exceeded

Solution code:
N,Q = input().split(" ")
sentence = input()

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

Submission status: Time Limit Exceeded

Solution code:
N,Q = input().split(" ")
sentence = input()

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

Submission status: Time Limit Exceeded

Solution code:
N,Q = input().split(" ")
sentence = input()

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

Submission status: Time Limit Exceeded

Solution code:
N,Q = input().split(" ")
sentence = input()

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

	print(count)

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,list(input().split()))
s=list(input())
b=[]
for i in range(n-1):
    if (s[i]=="A")and(s[i+1]=="C"):
        b.append(i)
for j in range(q):
    l,r=map(int,list(input().split()))
    print(len([k for k in b if l-1<=k<=r-2]))

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

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

# s = 'ACACTACG'
# lr = [
#     [3, 7],
#     [2, 3],
#     [1, 8],
# ]

wa = []
for index, val in enumerate(s):
    if "AC" == s[index - 1:index + 1]:
        wa.append(1)
    else:
        wa.append(0)

for row in lr:
    start = row[0]
    end = row[1]

    sss = sum(wa[start:end])
    print(sss)

Submission status: Time Limit Exceeded

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

# lr = [
#     [3, 7],
#     [2, 3],
#     [1, 8],
# ]

for row in lr:
    cnt = s.count('AC', row[0] - 1, row[1])
    print(cnt)

Submission status: Time Limit Exceeded

Solution code:
def main():
  N, Q = map(int, input().split())
  S = input()
  l, r, count = [], [], []
  for i in range(Q):
    L, R = map(int, input().split())
    l.append(L)
    r.append(R)
    flg = 0
    c = 0
#     print(S[l[i]-1:r[i]]) # これで狙った文字がでる
    T = S[l[i]-1:r[i]]
    for j in range(len(T)):
      if (T[j]=='A'):
        flg = 1
      if (flg==1) and (T[j]=='C'):
        c = c + 1
        flg = 0
    count.append(c)
  for i in range(Q):
    print(count[i])
main()

Submission status: Time Limit Exceeded

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

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

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
ACs_indices = [i for i in range(N-1) if S[i:i+2] == 'AC']

for _ in range(Q):
    l, r = map(int, input().split())
    ans = 0
    for idx in ACs_indices:
        if idx in range(l-1, r-1):
            ans += 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int,input().split())
s = input()
lr_list = []
#for i in range(q):
#    lr_list.append(list(map(int,input().split())))
ans_list = []
for i in range(q):
#    l,r = lr_list[i]
    l,r = map(int,input().split())
    ans_list.append(s[l-1:r].count('AC'))
for i in range(q):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int,input().split())
s = input()
lr_list = []
#for i in range(q):
#    lr_list.append(list(map(int,input().split())))
ans_list = []
for i in range(q):
#    l,r = lr_list[i]
    l,r = map(int,input().split())
    ans_list.append(s[l-1:r].replace('AC','1').count('1'))
for i in range(q):
    print(ans_list[i])

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int,input().split())
s = input()
lr_list = []
for i in range(q):
    lr_list.append(list(map(int,input().split())))
    
for i in range(q):
    l,r = lr_list[i]
    print(s[l-1:r].replace('AC','1').count('1'))

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = input()
    
    for i in range(Q):
        start, end = list(map(int, input().split()))
        s = S[start-1:end]
        
        cnt = 0
        while True:
            #print(s)
            
            f = s.find('AC')
            if f == -1:
                break
            else:
                cnt = cnt + 1
                s = s[:f] + s[f + 2:]
        
        print(cnt)
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, q = read_line(int, int)
s, = read_line(str)

m = [0 for _ in range(len(s) + 1)]

c = 0
for i in range(len(s)):
    if s[i:i+2] == 'AC':
        c += 1
    m[i + 1] = c

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1

    if c == 0:
        print(0)
        continue
    print(m[1:][r - 1] - m[l])

Submission status: Time Limit Exceeded

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, q = read_line(int, int)
s, = read_line(str)

p = []
i = 0
while i < len(s) - 1:
    if s[i:i+2] == 'AC':
        p.append(i)
        i += 2
    else:
        i += 1

m = [0 for _ in range(len(s) + 1)]

c = 0
for i in range(len(s)):
    if s[i:i+2] == 'AC':
        c += 1
    m[i + 1] = c

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1

    if c == 0:
        print(0)
        continue

    # print(m)
    # print(l, r)
    # print(m[1:][r - 1], m[l])
    print(m[1:][r - 1] - m[l])

    # print()

Submission status: Time Limit Exceeded

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, q = read_line(int, int)
s, = read_line(str)

p = []
i = 0
while i < len(s) - 1:
    if s[i] == 'A' and s[i+1] == 'C':
        p.append(i)
        i += 2
    else:
        i += 1

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1

    if len(p) == 0:
        print(0)
        continue

    c = 0
    for j in range(len(p)):
        pp = p[j]
        if l <= pp:
            break
        c += 1

    for jj in reversed(range(j, len(p))):
        pp = p[jj]
        if pp + 1 <= r:
            break
        c += 1

    print(len(p) - c)

Submission status: Time Limit Exceeded

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, q = read_line(int, int)
s, = read_line(str)

p = []
i = 0
while i < len(s) - 1:
    if s[i:i+2] == 'AC':
        p.append(i)
        i += 2
    else:
        i += 1

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1

    if len(p) == 0:
        print(0)
        continue

    c = 0
    for j in range(len(p)):
        pp = p[j]
        if l <= pp:
            break
        c += 1

    for jj in reversed(range(j, len(p))):
        pp = p[jj]
        if pp + 1 <= r:
            break
        c += 1

    print(len(p) - c)

Submission status: Time Limit Exceeded

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, q = read_line(int, int)
s, = read_line(str)

p = []
i = 0
while i < len(s) - 1:
    if s[i:i+2] == 'AC':
        p.append(i)
        i += 2
    else:
        i += 1

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1


    c = len(p)
    for j in range(len(p)):
        if not l <= p[j]:
            c -= 1
        if not (p[-j] + 1) <= r:
            c -= 1
        if j >= len(p) - j:
            break

    print(c)

Submission status: Time Limit Exceeded

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, q = read_line(int, int)
s, = read_line(str)

p = []
i = 0
while i < len(s) - 1:
    if s[i:i+2] == 'AC':
        p.append(i)
        i += 2
    else:
        i += 1

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1


    c = len(p)
    for j in range(len(p)):
        if not l <= p[j]:
            c -= 1

    for j in range(len(p)):
        if not (p[j] + 1) <= r:
            c -= 1

    print(c)

Submission status: Time Limit Exceeded

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, q = read_line(int, int)
s, = read_line(str)

p = []
i = 0
while i < len(s) - 1:
    if s[i:i+2] == 'AC':
        p.append(i)
        i += 2
    else:
        i += 1

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1

    c = 0
    for pp in p:
        if l <= pp and pp + 1 <= r:
            c += 1
    print(c)

Submission status: Time Limit Exceeded

Solution code:
def read_line(*types): return [f(a) for a, f in zip(input().split(), types)]

n, q = read_line(int, int)
s, = read_line(str)

p = []
for i in range(len(s) - 1):
    if s[i:i+2] == 'AC':
        p.append(i)

for i in range(q):
    l, r = read_line(int, int)
    l, r = l - 1, r - 1

    c = 0
    for pp in p:
        if l <= pp and pp + 1 <= r:
            c += 1
    print(c)

Submission status: Time Limit Exceeded

Solution code:
n,q = map(int,input().split())
base = input()
ans=[]
for i in range(q):
    l,r = map(int,input().split())
    ans.append(str(base[l-1:r].count("AC")))
print('\n'.join(ans))

Submission status: Time Limit Exceeded

Solution code:
import sys
import re
from collections import deque, defaultdict, Counter
from math import ceil, sqrt, hypot, factorial, pi, sin, cos, radians
from itertools import permutations, combinations, product
from operator import itemgetter, mul
from copy import deepcopy
from string import ascii_lowercase, ascii_uppercase, digits


def input(): return sys.stdin.readline().strip()


def INT(): return int(input())


def MAP(): return map(int, input().split())


def LIST(): return list(map(int, input().split()))


sys.setrecursionlimit(10 ** 9)
INF = float('inf')
MOD = 10 ** 9 + 7

N, Q = MAP()
S = input()
lr = [LIST() for _ in range(Q)]


def func(s, l, r):
    lis = []
    ret = 0
    for i in range(l-1, r):
        lis.append(s[i])

    for i, x in enumerate(lis):
        if(i == r-l):
            break
        else:
            if x == 'A' and lis[i + 1] == 'C':
                ret += 1

    return ret


for i in range(Q):
    print(func(S, lr[i][0], lr[i][1]))

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = input()
    
    for i in range(Q):
        start, end = list(map(int, input().split()))
        print(S[start-1:end].count('AC'))
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

Solution code:
import sys

def main():
    input = sys.stdin.readline
    N, Q = map(int, input().split())
    S = input()
    tr = [list(map(int, input().split())) for i in range(Q)]
    
    [print(S[l[0]-1:l[1]].count('AC')) for l in tr ]
    
if __name__ == '__main__':
    main()

Submission status: Time Limit Exceeded

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

for _ in range(q):
  i, r = map(int, input().split())
  print(sb[i-1:r].count(b'AC'))

Submission status: Time Limit Exceeded

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

pos = []
i = 0
while i < len(s):
    i = s.find('AC', i)
    if i == -1:
        break
    pos.append(i)
    i += 2

for _ in range(q):
    i, r = map(int, input().split())
    print(len([c for c in pos if i -1 <= c < r - 1]))

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input().rstrip()
for _ in range(q):
  i, r = map(int, input().split())
  print(s[i-1:r].count('AC'))

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 += [i]

for _ in range(Q):
  L = tuple(map(int, input().split()))

  c = 0
  for l in lis:
    if L[0] - 1 <= l < L[1] - 1:
      c+=1
      
  
  print(c)

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:i+2] == 'AC':
    lis += [i]

for _ in range(Q):
  L = tuple(map(int, input().split()))

  c = 0
  for l in lis:
    if L[0] - 1 <= l < L[1] - 1:
      c+=1
      
  
  print(c)

Submission status: Time Limit Exceeded

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

lis = []
p=0
while True:
  p = S.find("AC", p)

  if p < 0:
    break

  lis += [p]

  p+=2

for _ in range(Q):
  L = tuple(map(int, input().split()))

  c = 0
  for l in lis:
    if L[0] - 1 <= l < L[1] - 1:
      c+=1
      
  
  print(c)

Submission status: Time Limit Exceeded

Solution code:
import sys
import math

r=[x.strip() for x in sys.stdin.readlines()[1:]]

s=[c for c in r.pop(0)]

q=[x.split() for x in r]

for i in q:
    ans=0
    prt=s[int(i[0])-1:int(i[1])]
    f=0
    for c in prt:
        if c=="A":
            f=1
        elif c=="C":
            if f:
                ans+=1
            f=0
        else:
            f=0

    print(ans)

Submission status: Time Limit Exceeded

Solution code:
n, q = [int(i) for i in input().split()]
s = input()
positions = []

for i in range(1, n+1):
    if s[(i-1):(i+1)] == 'AC':
        positions.append(i)

for i in range(q):
    l, r = [int(i) for i in input().split()]
    print(len([i for i in positions if i in range(l, r)]))

Submission status: Time Limit Exceeded

Solution code:
n, q = [int(i) for i in input().split()]
s = input()
for i in range(q):
    l, r = [int(i) for i in input().split()]
    print(s[(l-1):r].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split())
S=input()
for _ in range(Q):
  L = tuple(map(int, input().split()))

  p = L[0] - 1
  c = 0
  while True:
    p = S.find("AC", p)

    if p < 0 or p >= L[1]-1:
      break

    c+=1
    p+=2
  print(c)

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split())
S=input()
L=[tuple(map(int, input().split())) for _ in range(Q)]
print("\n".join([str(len(S[nS-1:nE])-len(S[nS-1:nE].replace("AC", "|"))) for nS, nE in L]))

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())
  ans=0
  for j in range(l-1,r-1):
    if s[j]=='A' and s[j+1]=='C':
      ans+=1
  print(ans)

Submission status: Time Limit Exceeded

Solution code:
import sys

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

l = [0]*Q
r = [0]*Q
for i in range(Q):
	l[i],r[i]=map(int,input().split())
c = ['A','C']

for i in range(Q):
	ans = 0
	for j in range(r[i]-l[i]):
		if S[l[i]+j-1:l[i]+j+1] == c:
			ans += 1
	print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,Q = list(map(int,input().split()))
S = input()
for i in range(Q):
	f,t = list(map(int,input().split()))
	print(S[f-1:t].count('AC'))

Submission status: Time Limit Exceeded

Solution code:
#累積和による解法

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

for i in range(N+1):
    S_part = S[:i]
    cnt = S_part.count("AC")
    cnt_list.append(cnt)
for j in range(Q):
    l,r = map(int, input().split())
    ans = cnt_list[r]-cnt_list[l-1]
    if S[l-1:l+1]=="CA":
        ans -= 1
    print(ans)

Submission status: Time Limit Exceeded

Solution code:
N,Q=map(int,input().split()) 
S=str(input())
for _ in range(Q): 
    x,y = map(int, input().split()) 
    s=S[x-1:y]
    n=s.count('AC')
    print(n)

Submission status: Time Limit Exceeded

Solution code:
import sys


def inp():
    return sys.stdin.readline().strip()


n, q = map(int, inp().split())
s = inp()
a = []

for i in range(n - 1):
    if s[i:i+2] == 'AC':
        a.append(1)
    else:
        a.append(0)

for _ in range(q):
    l, r = map(int, inp().split())
    print(sum(a[l-1:r-1]))

Submission status: Time Limit Exceeded

Solution code:
n, q = map(int, input().split())
s = input()
a = [0] * (n - 1)

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

for _ in range(q):
    l, r = map(int, input().split())
    print(sum(a[l-1:r-1]))

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())
    print(s[l-1:r].count('AC'))

Submission status: Time Limit Exceeded

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

for i in range(len(term)):
    for j in range(term[i][1] - term[i][0] - 1):
        if S[j] == 'A':
            if S[j + 1] == 'C':
                times += 1
    print(times)
    times = 0

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

for i in range(q):
    f,e = (map(int, input().split()))
    st = s[f-1:e]
    count = 0
    for j in range(len(st)-1):
        if st[j] == 'A' and st[j+1] == 'C':
            count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

for i in range(q):
    f,e = (map(int, input().split()))
    st = s[f-1:e]
    # print(st.count('AC'))
    count = 0
    for j in range(len(st)-1):
        if st[j] == 'A' and st[j+1] == 'C':
            count += 1
    print(count)

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

for i in range(q):
    f,e = (map(int, input().split()))
    st = s[f-1:e]
    print(st.count('AC'))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

for i in range(q):
    l = list(map(int, input().split()))
    f,e = l[0]-1,l[1]
    st = s[f:e]
    print(st.count('AC'))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

for i in range(q):
    f,e = lst[i][0]-1,lst[i][1]
    st = s[f:e]
    print(st.count('AC'))

Submission status: Time Limit Exceeded

Solution code:
# coding: utf-8

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

count_list = []
for i in range(q):
    f,e = lst[i][0]-1,lst[i][1]
    st = s[f:e]
    count_list.append(st.count('AC'))

for i in count_list:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
import pdb
import builtins
import sys
import numpy as np
class Test:
    def __init__(self):
        pass

class CustomUtils:
    def __init__(self):
        pass
    def to_int(self, inputs, sp=' '):
        if sp == '':
            return [int(d) for d in list(inputs)]
        else:
            # return map(int,input().split())
            return [int(d) for d in inputs.split(sp)]
    def debug_print(self, inputs, debug=False):
        if debug:
            print(inputs)

# 引用　python する man,「Pythonでの再帰関数とメモ化」, PSM, ２０１７、７/４
# http://muromura.hatenablog.com/entry/2017/07/04/204908#%E3%83%A1%E3%83%A2%E5%8C%96
def memorize(f):
    table = {}
    def func(*args):
        if not args in table:
            table[args] = f(*args)
        return table[args]
    return func
debug = False

u = CustomUtils()

n, q = u.to_int(input())
s = input()

import re

ss = []
m = re.finditer('AC', s)
for match in m:
    st, en= match.span()
    ss.append(st)
ss.sort()
ss = np.array(ss)

# そこまでに含まれるACの数を返す。
@memorize
def accumulative(i):
    return sum(ss <= i - 1)
    
total = len(ss)

for _ in range(q):
    # n文字目とindexがずれた。
    l, r = [int(x)  - 1 for x in input().split()]
    result = accumulative(r) - accumulative(l)
    # ちょうどindexlまでに含まれるACの数が、境界をまたぐ数。
    print("{}".format(result))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    NQ = input().split(" ")
    N ,Q = int(NQ[0]),int(NQ[1])

    S = input()
    stack =[]
    for x in range(Q):
        lr = input()
        lr = lr.split(" ")
        l ,r = int(lr[0]),int(lr[1])
        target = S[l-1:r]
        stack.append((r-l+1-len(target.replace("AC"," "))))

    for x in stack:
        print(x)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    NQ = input().split(" ")
    N ,Q = int(NQ[0]),int(NQ[1])

    S = input()
    stack =[]
    for x in range(Q):
        lr = input()
        lr = lr.split(" ")
        l ,r = int(lr[0]),int(lr[1])
        target = S[l-1:r]
        stack.append((r-l+1-len(target.replace("AC","")))/2)
        
        
    for x in stack:
        print(int(x))

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    NQ = input().split(" ")
    N ,Q = int(NQ[0]),int(NQ[1])

    S = input()
    stack =[]
    for x in range(Q):
        lr = input()
        lr = lr.split(" ")
        l ,r = int(lr[0]),int(lr[1])
        target = S[l-1:r]
        num = target.split("AC")
        stack.append(len(num)-1)
    
    for x in stack:
        print(x)

Submission status: Time Limit Exceeded

Solution code:
if __name__ == "__main__":
    NQ = input().split(" ")
    N ,Q = int(NQ[0]),int(NQ[1])
 
    S = input()
    stack =[]
    for x in range(Q):
        lr = input()
        lr = lr.split(" ")
        l ,r = int(lr[0]),int(lr[1])
        target = S[l-1:r]
        num = target.split("AC")
        stack.append(num)
    for x in [len(y)-1 for y in stack]:
        print(x)

Submission status: Time Limit Exceeded

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

for i in range(Q):
        q.append([int(x) - 1 for x in input().split()])

#A = [0 for i in range(len(S))]

for p in q:
        M = S[p[0] : p[1]+1]
        print(M.count("AC"))

Submission status: Time Limit Exceeded

Solution code:
n,m=map(int,input().split())
s=input()
for i in range(m):
  a,b=map(int,input().split())
  count=0
  tmp=s[a-1:b]
  for j in range(len(tmp)-1):
    if tmp[j]=='A' and tmp[j+1]=='C':
      count+=1
  print(count)
  count=0

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())
    ss = s[l-1:r]
    ans.append(ss.count("AC"))
for i in ans:
    print(i)

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, 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] = map(int, input().split())
for i in range(Q):
        str_ = S[l[i]-1:r[i]]
        count = 0
        ac_flag = 0
        for chr in str_:
                if chr == 'A':
                        ac_flag = 1
                elif chr == 'C' and ac_flag == 1:
                        count += 1
                else:
                        ac_flag = 0
        print('{}'.format(count))

Submission status: Time Limit Exceeded

Solution code:
N, Q = map(int, input().split())
S = input()
while Q != 0:
        l, r = map(int, input().split())
        str_ = S[l-1:r]
        count = 0
        ac_flag = 0
        for chr in str_:
                if chr == 'A':
                        ac_flag = 1
                elif chr == 'C' and ac_flag == 1:
                        count += 1
                else:
                        ac_flag = 0
        print('{}'.format(count))
        Q -= 1

Submission status: Time Limit Exceeded

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

def solve(l, r):
    return sum(1 for i in range(l - 1, r - 1) if S[i : i + 2] == 'AC')

for i in range(Q):
    l, r = map(int, input().split())
    print(solve(l, r))

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
l=[0]*q
r=[0]*q
c=[0]*n
ans=0
for i in range(q):
    l[i],r[i]=map(int,input().split())
t=0
for i in range(n-1):
    if s[i]=='A' and s[i+1]=='C':
        c[t]=i+1
        t+=1
for i in range(q):
    t=0
    while(c[t]!=0):
        if c[t]>=l[i] and c[t]<r[i]:
            ans+=1
        t+=1
    print(ans)
    ans=0

Submission status: Time Limit Exceeded

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

Submission status: Time Limit Exceeded

Solution code:
n,q=map(int,input().split())
s=input()
l=[0]*q
r=[0]*q
c=[0]*q
for i in range(q):
    l[i],r[i]=map(int,input().split())
for i in range(q):
    for j in range(l[i],r[i]):
        if s[j-1]=='A' and s[j]=='C':
            c[i]+=1
for i in range(q):
    print(c[i])

Submission status: Time Limit Exceeded

Solution code:
length, questions = [int(a) for a in input().split()]
strings = str(input())
for j in range(questions):
    start, end = [int(a) for a in input().split()]
    moji = strings[start - 1: end]
    counts = 0
    place = 0
    temp = 0
    while True:
        temp = moji.find('AC', place)
        if temp != -1:
            counts += 1
            place = temp + 2
        else:
            break
    print(counts)

Submission status: Time Limit Exceeded

Solution code:
length, questions = [int(a) for a in input().split()]
strings = str(input())
for j in range(questions):
    start, end = [int(a) for a in input().split()]
    moji = strings[start - 1: end]
    counts = 0
    for k in range(len(moji) - 1):
        if moji[k] == 'A':
            if moji[k + 1] == 'C':
                counts += 1
    print(counts)

Submission status: Time Limit Exceeded

Solution code:
length, questions = [int(a) for a in input().split()]

strings = str(input())
for j in range(questions):
    start, end = [int(a) for a in input().split()]
    moji = strings[start - 1: end]
    counts = 0
    for k in range(len(moji) - 1):
        if moji[k] == 'A' and moji[k + 1] == 'C':
            counts += 1
    print(counts)