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: Accepted

Solution code:
import sys
import math
import collections
import itertools
import array
import inspect

# Set max recursion limit
sys.setrecursionlimit(1000000)


# Debug output
def chkprint(*args):
    names = {
        id(v): k
        for k, v in inspect.currentframe().f_back.f_locals.items()
    }
    print(', '.join(
        names.get(id(arg), '???') + ' = ' + repr(arg) for arg in args))


# Binary converter
def to_bin(x):
    return bin(x)[2:]


def li_input():
    return [int(_) for _ in sys.stdin.readline().split()]


def gcd(n, m):
    if n % m == 0:
        return m
    else:
        return gcd(m, n % m)


def gcd_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = gcd(v, L[i])

    return v


def lcm(n, m):
    return (n * m) // gcd(n, m)


def lcm_list(L):
    v = L[0]

    for i in range(1, len(L)):
        v = lcm(v, L[i])

    return v


# Width First Search (+ Distance)
def wfs_d(D, N, K):
    """
    D: 隣接行列(距離付き)
    N: ノード数
    K: 始点ノード
    """

    dfk = [-1] * (N + 1)
    dfk[K] = 0

    cps = [(K, 0)]
    r = [False] * (N + 1)
    r[K] = True
    while len(cps) != 0:
        n_cps = []
        for cp, cd in cps:
            for i, dfcp in enumerate(D[cp]):
                if dfcp != -1 and not r[i]:
                    dfk[i] = cd + dfcp
                    n_cps.append((i, cd + dfcp))
                    r[i] = True

        cps = n_cps[:]

    return dfk


# Depth First Search (+Distance)
def dfs_d(v, pre, dist):
    """
    v:  現在のノード
    pre: １つ前のノード
    dist: 現在の距離

    以下は別途用意する
    D: 隣接リスト(行列ではない)
    D_dfs_d: dfs_d関数で用いる，始点ノードから見た距離リスト
    """

    global D
    global D_dfs_d

    D_dfs_d[v] = dist

    for next_v, d in D[v]:
        if next_v != pre:
            dfs_d(next_v, v, dist + d)

    return


def sigma(N):
    ans = 0
    for i in range(1, N + 1):
        ans += i
    return ans


def comb(n, r):
    if n - r < r: r = n - r
    if r == 0: return 1
    if r == 1: return n

    numerator = [n - r + k + 1 for k in range(r)]
    denominator = [k + 1 for k in range(r)]

    for p in range(2, r + 1):
        pivot = denominator[p - 1]
        if pivot > 1:
            offset = (n - r) % p
            for k in range(p - 1, r, p):
                numerator[k - offset] /= pivot
                denominator[k] /= pivot

    result = 1
    for k in range(r):
        if numerator[k] > 1:
            result *= int(numerator[k])

    return result

def bisearch(L, target):
    low = 0
    high = len(L) - 1
    
    while low <= high:
        mid = (low + high) // 2
        guess = L[mid]
        if guess == target:
            return True
        elif guess < target:
            low = mid + 1
        elif guess > target:
            high = mid - 1
    if guess != target:
        return False

# --------------------------------------------

dp = None


def main():
    N, Q = li_input()
    S = input()

    A = [0] * N
    k = 0

    for i in range(1, N):
        if S[i-1:i+1] == "AC":
            k += 1
        A[i] = k

    for i in range(Q):
        l, r = li_input()

        print(A[r-1] - A[l-1])




main()

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
s = input()
m = [list(map(int,input().split())) for i in range(q)]
count = [0]*(n+1)
 
for i in range(n):
    if s[i:i+2] == 'AC':
        count[i+1] = count[i] + 1
    else:
        count[i+1] = count[i]
 
for l,r in m:
    print(count[r-1]-count[l-1])

Submission status: Accepted

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

comu = [0]*(N+1)

for i in range(N):
    if S[i:i+2] == 'AC':
        comu[i+1] = comu[i] + 1
    else:
        comu[i+1] = comu[i]

for l,r in list:
    print(comu[r-1]-comu[l-1])

Submission status: Accepted

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

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

ac = [0] * N
cnt = 0
for i in range(1, N):
    if S[i-1:i+1] == "AC":
        cnt += 1
    ac[i] = cnt
    
for l, r in lr:
    print(ac[r-1] - ac[l-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
t=[0]*(N+1)

for i in range(N):
  t[i + 1] = t[i] + (1 if S[i : i + 2] == 'AC' else 0)
for i in range(Q):
  l, r = map(int, input().split())
  print(t[r-1] - t[l-1])

Submission status: Accepted

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

t = [0] * (N + 1)
for i in range(N):
    t[i + 1] = t[i] + (1 if S[i:i+2] == 'AC' else 0)

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

Submission status: Accepted

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

ac_indicies = []
cp = S.find('AC')
while cp != -1:
    ac_indicies.append(cp)
    cp = S.find('AC', cp + 1)

min_i = 0
ac = ac_indicies + [N]
l_index = []
for i in range(N):
    if i > ac[min_i]:
        min_i += 1
    l_index.append(min_i)

total = len(ac_indicies)
for i in range(int(Q)):
    l, r = tuple(map(int, input().strip().split(' ')))
    print(l_index[r-1] - l_index[l-1])

Submission status: Accepted

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

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())

aclist = []

cnt = 0
for i in range(n):
    if s[i-1] == "A" and s[i] == "C":
        cnt += 1
        aclist.append(cnt)
    else:
        aclist.append(cnt)

for i in range(q):
    print(aclist[r[i]-1] - aclist[l[i]-1])

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
t = [0] * (N+1)
for i in range(0, N):
    if S[i:i+2] == 'AC':
        t[i+1] = t[i] + 1
    else:
        t[i+1] = t[i]
inputList = []
for i in range(1, Q+1):
    inputList.append(input())

for i in range(Q):
    x, y = map(int, inputList[i].split())
    res = t[y-1] - t[x-1]
    print(res)

Submission status: Accepted

Solution code:
N,Q = map(int,input().split())
s = input()
a = [0] * (N + 1)

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

Submission status: Accepted

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

    N = [0] * (n + 1)
    for i in range(n):
        N[i + 1] = N[i] + (1 if S[i:i + 2] == 'AC' else 0)

    print()
    
    for j in range(q):
        l, r = map(int, input().split())
        
        print(N[r-1] - N[l-1])
    
    
if __name__ == '__main__':
    main()

Submission status: Accepted

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

cum, now, bef = [], 0, False

for c in S:
    if bef and c == 'C':
        now += 1
    cum.append(now)
    if c == 'A':
        bef = True
    else:
        bef = False

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
Ls = len(S)
x = [0 for _ in range(Ls)]
for i in range(Ls - 1):
    x[i + 1] = x[i]
    if S[i:i + 2] == 'AC':
        x[i + 1] += 1
for i in range(Q):
    l, r = map(int, input().split())
    print(x[r - 1] - x[l - 1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
s = input()
a = [0] * (N + 1)

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

Submission status: Accepted

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

b = [0]
for j in range(n-1):
    if s[j] == 'A' and s[j+1] == 'C':
        b.append(b[j] + 1)
    else:
        b.append(b[j])
for i in range(q):
    l, r = lr[i]
    print(b[r-1] - b[l-1])

Submission status: Accepted

Solution code:
import re

N, Q = map(int, input().split())
S = input()
lr_list = [map(int, input().split()) for _ in range(Q)]

count_array = []

count = 0
is_first = True
for index in range(N):
    if index == 0:
        pass
    elif S[index - 1] == 'A' and S[index] == 'C':
        count += 1
    count_array.append(count)


for l, r in lr_list:
    right_count = count_array[r-1]
    left_count = count_array[l-1]
    print(right_count - left_count)

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
# 答案
N,Q = map(int,input().split())
S = input()
l,r = 0,0
csum = [0]*(N+1)

# 累積和
for ctr in range(N):
    if ctr == (N-1):
        csum[ctr+1] = csum[ctr]
    elif S[ctr]=='A' and S[ctr+1]=='C':
        csum[ctr+1] = csum[ctr] + 1
    else:
        csum[ctr+1] = csum[ctr]

# 出力
for ctr in range(Q):
    l,r=map(int,input().split())
    print(csum[r-1] - csum[l-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
l = [0] * Q
r = [0] * Q
for i in range(Q):
    l[i], r[i] = map(int, input().split())
counter = [0] * N

for i in range(N-1):
    if S[i:i+2] == 'AC':
        counter[i+1] += counter[i] + 1
    else:
        counter[i+1] += counter[i]

for i in range(Q):
    print(counter[r[i]-1] - counter[l[i]-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
l = [0] * Q
r = [0] * Q
for i in range(Q):
    l[i], r[i] = map(int, input().split())
counter = [0] * N

for i in range(N-1):
    if S[i:i+2] == 'AC':
        counter[i+1] += counter[i] + 1
    else:
        counter[i+1] += counter[i]

for i in range(Q):
    print(counter[r[i]-1] - counter[l[i]-1])

Submission status: Accepted

Solution code:
from itertools import accumulate

N, Q = map(int, input().split(" "))
S = input()
LR = [list(map(int, input().split(" "))) for _ in range(Q)]

p = (S[i:i+2]=="AC" for i in range(N))
p = [0] + list(accumulate(p))

# print(p)

for l, r in LR:
    l, r = l-1, r
    # print(S[l:r], l, r)
    print(p[r-1]-p[l])

Submission status: Accepted

Solution code:
#C問題  
N,Q=map(int,input().split())
S=str(input())
Slist=list(S)
pile=[0 for i in range(N)]
for i in range(N-1):
    if i == 0:
        pass
    else:
        pile[i]=pile[i-1]
  
    if Slist[i] == "A" and Slist[i+1] == "C":
        pile[i]+=1
pile[-1]=pile[-2]
pile.insert(0,0)
for i in range(Q):
    l,r=map(int,input().split())
    print(pile[r-1]-pile[l-1])

Submission status: Accepted

Solution code:
# coding:utf-8

memo = []

def main():
    N,Q = map(int,input().split())
    S = input()
    t = [0] *(N+1)
    for i in range(N):
        if S[i:i+2] == "AC":
            t[i+1] = t[i] + 1
        else:
            t[i+1] = t[i]
    # for i in range(N-1):
    #     if S[i:i+2] == "AC":
    #         memo.append(i)
    # print(memo)

    for i in range(Q):
        li,ri = map(int,input().split())
        print(t[ri-1] - t[li-1])
        # count = len(memo)
        # 両方の範囲に入る最初と最後
        # print(li,ri)
        # for i in range(li-1,ri-1):
        #     # print("debug"+str(i))
        #     if S[i] == "A" and S[i+1] == "C":
        #         count += 1
        #         start = i
        #         break
        # if start < 0:
        #     count = 0
        # else: 
        #     for i in reversed(range(li-1,ri+1)):
        #         if S[i-1] == "A" and S[i] == "C":
        #             end = i-1
        #             break
        #     print(start,end)
        #     count = memo.index(end) - memo.index(start) + 1


        # for i in range(len(memo)):
        #     if ac_check(i,li,ri):
        #         start = i
        #         break
        # if start < 0:
        #     count = 0
        # else:
        #     for i in reversed(range(len(memo))):
        #         if ac_check(i,li,ri):
        #             end = i
        #             break
        #     # print(start,end)
        #     count = count - (start) - (len(memo)-end) + 1
        # print(count)

# def ac_check(i,li,ri):
#     if memo[i] >= li-1 and memo[i]+1 <= ri-1:
#         return True
#     return False

# def ac_check(sub_s):
#     count = 0
#     for i in range(len(sub_s)-1):
#         if sub_s[i:i+2] == "AC":
#             count += 1
#     return count


if __name__ == "__main__":
    main()

Submission status: Accepted

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

for i in range(Q):
    l_i, r_i = map(int, input().split())
    print(t[r_i-1] - t[l_i-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
t = [[int(x) for x in input().split()] for _ in range(q)]
add_list = [0]
for i in range(len(s)-1):
    if s[i:i+2] == "AC":
        add_list.append(add_list[i]+1)
    else:
        add_list.append(add_list[i])
for i in t:
    ans = add_list[i[1]-1] - add_list[i[0]-1]
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

Solution code:
# -*- coding: utf-8 -*-
n,q = map(int, input().split())
s = input()
lr = [list(map(int, input().split())) for _ in range(q)]

acc = []
for i in range(n+1):
    if i==0:
        acc.append(0)
    else:
        if s[i-2:i]=="AC":
            acc.append(acc[-1]+1)
        else:
            acc.append(acc[-1])
for l,r in lr:
    print(acc[r]-acc[l])

Submission status: Accepted

Solution code:
import bisect

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
a = [0] * N
for i in range(1, N):
    if S[i - 1 : i + 1] == 'AC':
        a[i] = a[i - 1] + 1
    else:
        a[i] = a[i - 1]
for i in range(Q):
    l, r = map(int, input().split())
    print(a[r - 1] - a[l - 1])

Submission status: Accepted

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

li_flg = [0] * len(s)

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


for idx, v in enumerate(s):
    if v == 'C' and idx != 0 and s[idx - 1] == 'A':
        li_flg[idx] = li_flg[idx - 1] + 1
    else:
        li_flg[idx] = li_flg[idx - 1]

for l in li_lr:
    print(li_flg[l[1] - 1] - li_flg[l[0] - 1])

Submission status: Accepted

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

t = [0] * (n + 1)

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

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

Submission status: Accepted

Solution code:
def main():
	n, q = map(int, input().split())
	s = input()
	acs = [0]*(n)
	for i in range(1, n):
		if s[i-1] == "A" and s[i] == "C":
			acs[i] = acs[i-1] + 1
		else:
			acs[i] = acs[i-1]
	results = [0]*q
	for i in range(q):
		l, r = map(int, input().split())
		results[i] = acs[r-1] - acs[l-1]
	for result in results:
		print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

Solution code:
def main():
	n, q = map(int, input().split())
	s = input()
	acs = [0]*(n)
	for i in range(1, n):
		if s[i-1] == "A" and s[i] == "C":
			acs[i] = acs[i-1] + 1
		else:
			acs[i] = acs[i-1]
	results = [0]*q
	for i in range(q):
		l, r = map(int, input().split())
		if l == r:
			continue
		results[i] = acs[r-1] - acs[l-1]
	for result in results:
		print(result)

if __name__ == '__main__':
    main()

Submission status: Accepted

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

for i in range(N-1):
    if S[i] == "A" and S[i+1] == "C":
        t[i+1] = t[i] + 1
    else:
        t[i+1] = t[i]
for l, r in lr:
    print(t[r-1]-t[l-1])

Submission status: Accepted

Solution code:
def main():
    n, q  = list(map(int, input().split()))
    s = list(input())
    queries = [list(map(int, input().split())) for _ in range(q)]
    
    dp = [0 for _ in range(n)] # 0文字目からi文字目に含まれるACの数

    for i in range(n - 1):
    	dp[i + 1] = dp[i] + ((s[i] + s[i + 1]) == "AC")
    
    for query in queries:
    	print(dp[query[1] - 1] - dp[query[0] - 1])

if __name__ == '__main__':
    main()

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
S = list(input())
a = [0]*n

for i in range(1,n):
    a[i] = a[i-1]
    if (S[i-1]=='A')and(S[i]=='C'):
        a[i] += 1 
for i in range(q):
    l,r = map(int,input().split())
    print(a[r-1]-a[l-1])

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
LR = [[int(i) for i in input().split()] for i in range(Q)]
pre = [0]*(N+1)
for i in range(N):
  pre[i+1] = pre[i] + (1 if S[i:i+2] == 'AC' else 0)
for a in LR:
  print(pre[a[1]-1] - pre[a[0]-1])

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random
#

# n,q = map(int,input().split())
# s = list(str(input()))
# L = []
# for i in range(q):
#     l,r = map(int,input().split())
#     L.append([l,r])
#
# # count_ac = np.zeros([n])
# count_ac = []
# count = 0
# i = 0
# while i < n-1:
#     if s[i] == "A" and s[i+1] == "C":
#         count_ac.append(count)
#         count += 1
#         count_ac.append(count)
#         i += 2
#     else:
#         count_ac.append(count)
#         i += 1
#
# count_ac.append(count)
#
#
# # print(count_ac)
# for i in range(q):
#     ans = count_ac[L[i][1]-1]-count_ac[L[i][0]-1]
#     print(int(ans))

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

Submission status: Accepted

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

# print("N:", N)
# print("S:", S)
# print("my_list:", my_list)

my_str_list = [0]
count = 0
for i in range(1, len(S)):
    if (S[i] == 'C' and S[i-1] == 'A'):
        count = count + 1
    my_str_list.append(count)
#print(my_str_list)

for i in range(Q):
    (l, r ) = my_list[i]
#    print(l, r)
    ret = my_str_list[r-1] - my_str_list[l-1]
    if (S[l-2] == 'A'):
#        ret = ret - 1
        ret = 0 if ret < 0 else ret
    print(ret)

Submission status: Accepted

Solution code:
N, Q = map(int, input().split(" "))
S = input()
st, en = [], []
for i in range(Q) :
    _st, _en = map(int, input().split(" "))
    st.append(_st)
    en.append(_en)
    
prev_char = ''
cnt = 0
cum_list = []
for c in S :
    if prev_char == 'A' and c == 'C' :
        cnt += 1
    cum_list.append(cnt)
    prev_char = c
    
for s, e in zip(st, en) :
    print(cum_list[e - 1] - cum_list[s - 1])

Submission status: Accepted

Solution code:
# encoding:utf-8
import copy
import numpy as np
import random

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

# count_ac = np.zeros([n])
count_ac = []
count = 0
i = 0
while i < n-1:
    if s[i] == "A" and s[i+1] == "C":
        count_ac.append(count)
        count += 1
        count_ac.append(count)
        i += 2
    else:
        count_ac.append(count)
        i += 1

count_ac.append(count)


# print(count_ac)
for i in range(q):
    ans = count_ac[L[i][1]-1]-count_ac[L[i][0]-1]
    print(int(ans))

Submission status: Accepted

Solution code:
n,m=map(int,input().split())
l=list(input())
l1=[0]
p=0
for i in range(n-1):
  if l[i]=="A" and l[i+1]=="C":
    p+=1
    l1.append(p)
  else:
    l1.append(p)
for t in range(m):
  i,j=map(int,input().split())
  print(l1[j-1]-l1[i-1])

Submission status: Accepted

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

tmp = [0 for _ in range(N)]

for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        tmp[i+1] = tmp[i] + 1
    else:
        tmp[i+1] = tmp[i]
        
for l in L:
    print(tmp[l[1]-1] - tmp[l[0]-1])

Submission status: Accepted

Solution code:
import numpy as np
N, Q = map(int, raw_input().split())
S = raw_input()
L = np.zeros(Q)
R = np.zeros(Q)
for i in range(Q):
    L[i], R[i] = map(int, raw_input().split())
num = np.zeros(N)
num_tmp = 0
for i in range(1,N):
    if S[i-1:i+1] == "AC":
        num_tmp += 1
    num[i] = num_tmp
for i in range(Q):
    print int(num[int(R[i]-1)] - num[int(L[i]-1)])

Submission status: Accepted

Solution code:
import numpy as np

N, Q = map(int, input().split())  # 複数数値入力
S = list(input())  # 一文字ずつ格納

dp = [0] * N
i = 0
flg = False
for str in S:
    if str == "C" and flg == True:
        dp[i] = dp[i - 1] + 1
        flg = False
    elif str == "A":
        flg = True
        dp[i] = dp[i-1]
    else:
        flg = False
        dp[i] = dp[i-1]
    i += 1

for i in range(Q):
    l, r = map(int, input().split())  # 複数数値入力
    print(dp[r-1]-dp[l-1])

Submission status: Accepted

Solution code:
ache = None                                                         
                                                                     
def make_cache(string):                                              
    global cache                                                     
                                                                     
    n = len(string)                                                  
    cache = [-2] * n                                                 
                                                                     
    prev = None                                                      
    count = 0                                                        
                                                                     
    for i, c in enumerate(string):                                   
        if c == 'C' and prev == 'A':                                 
            count += 1                                               
                                                                     
        cache[i] = count                                             
        prev = c                                                     
                                                                     
                                                                     
if __name__ == "__main__":                                           
    N, Q = map(int, input().split())                                 
    string = input()                                                 
    make_cache(string)                                               
                                                                     
    for _ in range(Q):                                               
        l, r = map(int, input().split())                             
        print(cache[r-1] - cache[l-1])

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=input()
t=[0]*(N+1)
for i in range(N):
  t[i+1]=t[i]+(1 if S[i:i+2]=="AC" else 0)
for i in range(Q):
  start,end=map(int,input().split())
  print(t[end-1]-t[start-1])

Submission status: Accepted

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

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
S = [0]
for i in range(n-1):
    if s[i] == "A" and s[i+1] == "C":
            S.append(S[-1]+1)
    else:
        S.append(S[-1])

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

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
S = [0]
last = "!"
for i in range(n):
    if last == "A" and s[i] == "C":
        S.append(S[-1]+1)
    else:
        S.append(S[-1])
    last = s[i]

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

Submission status: Accepted

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

cs = [0] * N
s = 0
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        s += 1
    cs[i+1] = s 

for l, r in lrs:
    print(cs[r-1] - cs[l-1])

Submission status: Accepted

Solution code:
import numpy as np

def count(s):
    prev = ""
    cnt = 0
    dp = np.zeros(len(s)+1, dtype=int)
    for i, c in enumerate(s):
        if (prev == "A") and (c == "C"):
            cnt += 1
        prev = c
        dp[i+1] = cnt
    return dp

def solve(n, q, s, l, r):
    dp = count(s)
    for i in range(q):
        print(dp[r[i]] - dp[l[i]])

n, q = map(int, input().split())
s = input()
l = np.zeros(q, dtype=int)
r = np.zeros(q, dtype=int)
for i in range(q):
    l[i], r[i] = map(int, input().split())
solve(n, q, s, l, r)

Submission status: Accepted

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

for i in range(N - 1):
    cnt[i + 1] = cnt[i] + (1 if S[i:i + 2] == "AC" else 0)


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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
cnt = [0] * (N + 1)

for i in range(N):
    cnt[i + 1] = cnt[i] + (1 if S[i:i + 2] == "AC" else 0)

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

Submission status: Accepted

Solution code:
N,Q=map(int,input().split())
S=input()
t=[0]*(N+1)
for i in range(N):
    t[i+1]=t[i]+(1 if S[i : i+2]=='AC' else 0)
for i in range(Q):
    l,r=map(int,input().split())
    print(t[r-1]-t[l-1])

Submission status: Accepted

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

T = "AC"
t = [0] * (N+1)

for val in range(N):
	if S[val:val+2] == "AC":
		t[val + 1] = t[val] + 1
	else:
		t[val + 1] = t[val]

for val in range(Q):

	print(t[lr[val][1]-1] - t[lr[val][0]-1])

Submission status: Accepted

Solution code:
N,Q = map(int,input().split())
slis = list(input()) + ['']
numbers = [0] * N
cur = 0
for i in range(1,N):
    if slis[i-1]=='A' and slis[i] == 'C':
        cur += 1
    numbers[i] = cur
        
for _ in range(Q):
    l,r = map(int,input().split())
    print(numbers[r-1]-numbers[l-1])

Submission status: Accepted

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

Submission status: Accepted

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

t = [0 for i in range(N+1)]
for i in range(N):
    t[i+1] = t[i] + (1 if S[i:i+2] == 'AC' else 0)
for i in range(Q):
    l, r = [int(s) for s in input().split()]
    print(t[r-1] - t[l-1])

Submission status: Accepted

Solution code:
# 解答通り
N, Q = map(int, input().split())
S = input()
t = [0] * (N+1)

for i in range(N):
    t[i+1] = t[i] + (1 if S[i:i+2] == "AC" else 0)

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

Submission status: Accepted

Solution code:
import numpy as np

a,b = list(map(int,input().split()))
S = input()
sub= "AC"
l = np.zeros(len(S)+1)
for i in range(len(S)):
  l[i+1] = l[i] + (1 if S[i:i+2]=="AC" else 0)

for i in range(b):
    r,t = list(map(int,input().split()))
    n = l[t-1]-l[r-1]
    print(int(n))

Submission status: Accepted

Solution code:
round = lambda x:(x*2+1)//2
a,b = list(map(int,input().split()))
S = input()
sub= "AC"
l = [0]*(len(S)+1)
for i in range(len(S)):
  l[i+1] = l[i] + (1 if S[i:i+2]=="AC" else 0)

for i in range(b):
    r,t = list(map(int,input().split()))
    n = l[t-1]-l[r-1]
    print(int(n))

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
L = [0]*Q 
R = [0]*Q
for q in range(Q):
    l, r = map(int, input().split())
    L[q] = l
    R[q] = r
ans = [0]*Q 
accumulative_count = [0]*(N) 
accumulative_count[0] = 0 
for i in range(1, N, 1): 
    accumulative_count[i] = accumulative_count[i-1]
    if (S[i-1:i] == 'A') & (S[i:i+1] == 'C'):
        accumulative_count[i] = accumulative_count[i-1] + 1
for q in range(Q):
    print(accumulative_count[R[q]-1] - accumulative_count[L[q]-1])