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:
n,q=map(int,input().split())
s=input()
list_ac=[0]
number=0
for i in range(0,n):
    if s[i:i+2]=='AC':
        number=number+1
        list_ac.append(number)
    else:
        list_ac.append(number)
for i in range(0,q):
    l,r=map(int,input().split())
    print(list_ac[r-1]-list_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
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 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 = str(input())
position = [0] * N
count = 0
for i in range(N-1):
    if S[i] == "A" and S[i + 1] == "C":
        count += 1
    position[i+1] = count
answers = []
for i in range(Q):
    l, r = map(int, input().split())
    l -= 1
    r -= 1
    answer = position[r] - position[l]
    answers.append(answer)
for i in range(Q):
    print(answers[i])

Submission status: Accepted

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

counts = [0] * n
count = 0
for i in range(1,n):
    if s[i] == "C" and s[i-1] == "A":
        count += 1
    counts[i] = count
counts = [0] + counts


for i in range(q):
    st, en = qs[i]
    print(counts[en] - counts[st])

Submission status: Accepted

Solution code:
I=lambda:map(int,input().split());n,q=I();s=input();a=[0]
for i in range(n):a+=a[i]+(s[i-1:i+1]=='AC'),
while q:l,r=I();print(a[r]-a[l]);q-=1

Submission status: Accepted

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

counts = [0] * n
count = 0
for i in range(1,n):
    if s[i] == "C" and s[i-1] == "A":
        count += 1
    counts[i] = count
counts = [0] + counts

for i in range(q):
    st, en = qs[i]
    print(counts[en] - counts[st])

Submission status: Accepted

Solution code:
n,q=map(int,input().split())
s=input()
ac=[0]*(n+1)
for i in range(n):
  ac[i]=ac[i-1]+(s[i:i+2]=="AC")
for i in range(q):
  l,r=map(int,input().split())
  print(ac[r-2]-ac[l-2])

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n, k = map(int, input().split())
s = input()
rr = (list(map(int, input().split())) for _ in range(k))

from collections import Counter

dc = [0, 0]
c = 0
for i in range(1, len(s)):
  if s[i-1]+s[i] == 'AC':
    c += 1
  dc.append(c)
for a, b in rr:
  print(dc[b] - dc[a])

Submission status: Accepted

Solution code:
# C - GeT AC
# https://atcoder.jp/contests/abc122/tasks/abc122_c

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

cnt = [0] * (len(S) + 1)

for s_id in range(len(S)):
    cnt[s_id + 1] = cnt[s_id]
    if S[s_id] == 'C' and S[s_id - 1] == 'A':
        cnt[s_id + 1] += 1

for _ in range(Q):
    l, r = map(int, raw_input().split())
    print '{}'.format(cnt[r] - cnt[l])

Submission status: Accepted

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

pair = [0] * len(S)
for i in range(len(S)-1):
    if S[i] is 'A' and S[i+1] is 'C':
        pair[i+1] = pair[i] + 1
    else:
        pair[i+1] = pair[i]

for i in range(Q):
    ans = 0
    l = lr[i][0]-1
    r = lr[i][1]-1
    print(pair[r]-pair[l])

Submission status: Accepted

Solution code:
from __future__ import print_function
import sys
import numpy as np

def eprint(*args, **kwargs):
    print(*args, file=sys.stderr, **kwargs)

N,Q = map(int,input().split())  # Nは文字列Sの長さ．Qは質問の個数．
S = input()
S=S+"0"

cnt=np.zeros(100000,dtype='int')
now_x=0
for i in range(1,N):
    if S[i-1]=='A' and S[i]=='C':
        cnt[i]=now_x+1
        now_x=cnt[i]
    else:
        cnt[i]=now_x

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
n, q = map(int, input().split())
s = input()

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

# print(count)

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

Submission status: Accepted

Solution code:
N, Q = map(int, input().split())
S = input()
num = {}
n = 0

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

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

Submission status: Accepted

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

Submission status: Accepted

Solution code:
gene = ['A','C','G','T']

n,q = map(int,input().split())
s = input()
l_r = []
for _ in range(q):
    l_r.append(list(map(int,input().split())))
imos = [0]*n
csum = [0]*n

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

for i in range(1,n):
    csum[i] = csum[i-1] + imos[i]

for l,r in l_r:
    print(csum[r-1]-csum[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 = list(map(int, input().split()))

s = input()
plc = [0] * n

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

Submission status: Accepted

Solution code:
#abc122c

n,q=map(int,raw_input().split())
s=raw_input()
t=[0]*(n+1)
for i in xrange(n):
 if s[i:i+2]=='AC':
  t[i+1]=t[i]+1
 else:
  t[i+1]=t[i]
for i in xrange(q):
 l,r=map(int,raw_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)
for i in range(N-1):
    t[i+1] = t[i] + (1 if S[i:i+2]=="AC" else 0)

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

Submission status: Accepted

Solution code:
if __name__ == '__main__':
    length, question_count = map(int, input().split())

    string = input()
    ac_sums = [0]
    for i in range(1, len(string) + 1):
        if i > 1 and string[i - 2] == 'A' and string[i - 1] == 'C':
            ac_sums.append(ac_sums[i - 1] + 1)
        else:
            ac_sums.append(ac_sums[i - 1])

    for i in range(question_count):
        l, r = map(int, input().split())
        ans = ac_sums[r] - ac_sums[l - 1]
        if l > 1 and string[l - 1] == 'C' and string[l - 2] == 'A':
            ans -= 1
        print(ans)

Submission status: Accepted

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

# 0-index
for i in range(q):
    for j in range(2):
        lr[i][j] -= 1

# 'AC'のカウントの累積和
ac_cnt = [0 for i in range(n)]
for i in range(n-1):
    ac_cnt[i+1] = ac_cnt[i] + (1 if s[i:i+2] == 'AC' else 0)

for i in range(q):
    print(ac_cnt[lr[i][1]] - ac_cnt[lr[i][0]])

Submission status: Accepted

Solution code:
N,Q=map(int,(input()).split())
S=input()
Count=[0]
count=0
for i in range(1,N):
  if S[i-1:i+1]=="AC":
    count+=1
    Count.append(count)
  else:
    Count.append(count)
for i in range(Q):
  l,r=map(int,input().split())
  print(Count[r-1]-Count[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 for i in range(len(s))]
count = 0
for j in range(len(s)-1):
    if s[j] == "A" and s[j+1] == "C":
        count += 1
    t[j+1] = count

for k 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:
import sys

s2nn = lambda s: [int(c) for c in s.split(' ')]

ss2nn = lambda ss: [int(s) for s in list(ss)]
ss2nnn = lambda ss: [s2nn(s) for s in list(ss)]

i2s = lambda: sys.stdin.readline().rstrip()
i2n = lambda: int(i2s())
i2nn = lambda: s2nn(i2s())

ii2ss = lambda n: [sys.stdin.readline() for _ in range(n)]

def main(N, Q, S, lrs):
    S2 = [0] * (N)
    for i in range(1, N):
        if S[i-1] == 'A' and S[i] == 'C':
            S2[i] = S2[i-1] + 1
        else:
            S2[i] = S2[i-1]
    for l, r in lrs:
        count = S2[r-1] - S2[l-1] 
        print(count)

N, Q = i2nn()
S = i2s()
lr = ss2nnn(ii2ss(Q))
main(N, Q, S, lr)

Submission status: Accepted

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

Submission status: Accepted

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

Submission status: Accepted

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

AC_appearance = [0] * len(S)
counter = 0
for i in range(1, len(S)):
    if S[i-1] == 'A' and S[i] == 'C':
        counter += 1
    AC_appearance[i] = counter 

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

Submission status: Accepted

Solution code:
import sys
input = sys.stdin.readline
def ii(): return int(input())
def mi(): return map(int, input().rstrip().split())
def lmi(): return list(map(int, input().rstrip().split()))
def li(): return list(input().rstrip())
# template


N, Q = mi()
# print(N, Q)
S = li()
Alist = []
for i in range(N - 1):
    if S[i] == 'A' and S[i + 1] == 'C':
        Alist.append(i)
Aset = set(Alist)
AClist = [0]*(N+1)
for i in range(N-1):
    if i in Aset:
        AClist[i + 2] = AClist[i + 1] + 1
    else:
        AClist[i+2] = AClist[i+1]
        # print(AClist)

for i in range(Q):
    l, r = mi()
    if AClist[l - 1] != AClist[l]:
        print(AClist[r] - AClist[l])
    else:
        print(AClist[r] - AClist[l-1])

Submission status: Accepted

Solution code:
n,q=map(int, raw_input().split())
s=list(raw_input())
cum=0
cum_array=[0]
for p in range(len(s)-1):
    if s[p] == 'A' and s[p+1] == 'C':
        cum+=1
    cum_array.append(cum)
cum_array.append(cum)
for i in range(q):
    l,r=map(int, raw_input().split())
    print(cum_array[r-1]-cum_array[l-1])

Submission status: Accepted

Solution code:
import sys
n,q=input().split()
s=input();a=[0]*2
for i in range(2,int(n)+1):
 a+=[a[i-1]+s[i-2:i].count("AC")]
for e in sys.stdin:
 l,r=map(int,e.split())
 print(a[r]-a[l])

Submission status: Accepted

Solution code:
import numpy as np

N,Q=map(int,input().split())
y=input()
arr=np.zeros(len(y))
for i in range(len(y)-1):
    if y[i:i+2]=='AC':
        arr[i+1]=arr[i]+1
    else:
        arr[i+1]=arr[i]
              
for q in range(Q):
    l,r=map(int,input().split())
    print(int(arr[r-1]-arr[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=str(input())
a=[0]*(n+1)
a[0]=0
for i in range(n):
    if s[i:i+2]=="AC":
        a[i+1]=a[i]+1
    else:
        a[i+1]=a[i]+0
for i in range(q):
    b,c=map(int,input().split())
    print(int(a[c-1]-a[b-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:
l = input().split()
N,Q = int(l[0]),int(l[1])
S = input()
blue = [0] * N
for k in range(1,N):
  if S[k-1] == 'A' and S[k] == 'C':
    blue[k] = blue[k-1] + 1
  else:
    blue[k] = blue[k-1]
for i in range(1,Q+1):
  rl = input().split()
  r = int(rl[0])
  l = int(rl[1])
  print(
    blue[l-1] - blue[r-1])

Submission status: Accepted

Solution code:
N, Q = [int(x) for x in 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(Q):
    l, r = [int(x) for x in input().split()]
    print(t[r-1] - t[l-1])

Submission status: Accepted

Solution code:
def main():
    n, q = map(int, input().split())
    s = input()
    p = prepare(s)
    for _ in range(q):
        l, r = map(int, input().split())
        print(query(p, l, r))

def prepare(s):
    p = [0]
    for i in range(len(s) - 1):
        if s[i:].startswith('AC'):
            p.append(p[-1] + 1)
        else:
            p.append(p[-1])
    return p

def query(p, l, r):
    return p[r - 1] - p[l - 1]

main()

Submission status: Accepted

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

dp = [0]*len(S)
for i in range(1, len(S)):
    dp[i] += dp[i-1]
    if S[i-1:i+1] == 'AC':
        dp[i]+=1


for _ in range(Q):
    [l,r] = list(map(int, input().split()))
    l-=1
    r-=1
    ans = dp[r] - (0 if l == 0 else dp[l-1])
    if l > 0 and S[l-1:l+1] == 'AC':
        ans -= 1
    print(ans)

Submission status: Accepted

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

Submission status: Accepted

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

num_list = []
cnt = 0
for i in range(N-1):
    if S[i:i+2] == 'AC':
        cnt += 1
    num_list.append(cnt)

for i in range(Q):
    if LR[i][0] == 1:
        l = 0
    else:
        l = num_list[LR[i][0]-2]
    if LR[i][1] == N:
        r = num_list[N-2]
    else:
        r = num_list[LR[i][1]-2]
    print(r-l)

Submission status: Accepted

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

for i in range(1,N):
    if(S[i-1]+S[i]=='AC'):
        cnt += 1
    A.append(cnt)

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

Submission status: Accepted

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

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

for i in range(Q):
    print(ac_place_list[tmp_list[i][1]-1]-ac_place_list[tmp_list[i][0]-1])

Submission status: Accepted

Solution code:
n,q = map(int,input().split())
s = input()
x = []
y = []
for i in range(q):
    x1,y1=[int(i) for i in input().split()]
    x.append(x1)
    y.append(y1) 

t = [0]*n

for i in range(n-1):
    if s[i+1] == "C" and s[i] == "A":
        t[i+1] = t[i]+1
    else:
        t[i+1] = t[i]
for i in range(q):
    ans = t[y[i]-1] - t[x[i]-1]
    print(ans)

Submission status: Accepted

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

def solve():
    N, Q = list(map(int, input().split(" ")))
    S = input()
    L, R = [], []
    for i in range(Q):
        l, r = list(map(int, input().split(" ")))
        L.append(l)
        R.append(r)
    
    # 累積話で解く
    accumulations = [0] * N

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

    for i in range(Q):
        if L[i]-1 == 0:
            print(accumulations[R[i]-1])
            continue
        else:
            print(accumulations[R[i]-1] - accumulations[L[i]-1])
            continue


if __name__ == "__main__":
    solve()

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
import numpy as np

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

line = np.zeros([n,])
count = 0
for i in range(1,n):
    if (s[i-1:i+1]=="AC"): 
        count += 1
    line[i]=count 
        
for i in range(q):
    l,r = map(int,input().split())
    target = line[r-1] - line[l-1]
    print(int(target))

Submission status: Accepted

Solution code:
import itertools
n,q = map(int,input().split())
s = input()
lr = []
for i in range(q): lr.append(list(map(int, input().split())))
ac = [1 if i!=0 and 'AC'==s[i-1:i+1] else 0 for i in range(0,len(s))]
acc = list(itertools.accumulate(ac))
for i in lr: print(acc[i[1]-1]-acc[i[0]-1])

Submission status: Accepted

Solution code:
n,q = map(int, input().split())
s = input()
counter = 0
countlist = [0] * 2

for i in range(n - 1):
    if s[i] == "A" and s[i + 1] == "C":
        counter += 1
    countlist += [counter]

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

Submission status: Accepted

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

for i in range(1, n):
    if s[i] == "C" and s[i - 1] == "A":
        counter += 1
    countlist += [counter]

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

Submission status: Accepted

Solution code:
N, Q = list(map(int, input().split()))
S = input()
c = 0
cl = [0 for i in range(N)]
cr = [0 for i in range(N)]
for i in range(0, N-1):
  cl[i+1] = cl[i]
  if S[i:i+2]=="AC":
    c += 1
    cl[i+1] += 1
for i in range(N-1, 0, -1):
  cr[i-1] = cr[i]
  if S[i-1:i+1]=="AC":
    cr[i-1] += 1
for i in range(Q):
  li, ri = list(map(int, input().split()))
  print(c-cl[li-1]-cr[ri-1])

Submission status: Accepted

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

for i in range(n):
    if s[i-1:i+1] == 'AC':
        count+=1
    counts[i] = count


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

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
S = input()
 
counts = [0 for i in range(n)]
cnt = 0
 
for i in range(n):
  if S[i-1:i+1] == "AC":
    cnt += 1
  counts[i] = cnt
    
for i in range(q):
    l, r = map(int, input().split())
    print(counts[r-1] - counts[l-1])

Submission status: Accepted

Solution code:
n, q = map(int, input().split())
s = input()
ruisekiwa = [0]
for i in range(len(s)-1):
    if s[i:i+2] == 'AC':
        ruisekiwa += [ ruisekiwa[-1] + 1 ]
    else:
        ruisekiwa += [ ruisekiwa[-1] ]

ruisekiwa += [ ruisekiwa[-1] ]

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

Submission status: Accepted

Solution code:
from sys import stdin
n, q = [int(_) for _ in stdin.readline().rstrip().split()]
s = list(stdin.readline().rstrip())
data = [list(map(int, stdin.readline().rstrip().split())) for _ in range(q)]
l = [0]
for i in range(n):
    l.append(l[i]+1 if i+1 < n and s[i] == 'A' and s[i+1] == 'C' else l[i])
for d in data:
    print(l[d[1]-1]-l[d[0]-1])

Submission status: Accepted

Solution code:
N,Q=map(int,(input()).split())
S=input()
l_list=[]
r_list=[]
for i in range(Q):
  l,r=input().split()
  l_list.append(int(l))
  r_list.append(int(r))
count=0
AC_count=[0]
for i in range(1,N+2):
  if S[i-1:i+1]=="AC":
    count+=1
  AC_count.append(count)
k=0
for j in range(Q):
  k=AC_count[r_list[j]-1]-AC_count[l_list[j]-1]
  print(k)

Submission status: Accepted

Solution code:
N,Q=map(int,(input()).split())
S=input()
l_list=[]
r_list=[]
for i in range(Q):
  l,r=input().split()
  l_list.append(int(l))
  r_list.append(int(r))
count=0
AC_count=[0]
for i in range(1,N+2):
  if S[i-1:i+1]=="AC":
    count+=1
  AC_count.append(count)
AC_count.append(count)
k=0
for j in range(Q):
  k=AC_count[r_list[j]-1]-AC_count[l_list[j]-1]
  print(k)

Submission status: Accepted

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

for i in range(q):
  print(l[x[i][1]-1]-l[x[i][0]-1])

Submission status: Accepted

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

Submission status: Accepted

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

cnt = 0
before = [0] * len(s)

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

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

Submission status: Accepted

Solution code:
from sys import stdin

n, q = map(int, stdin.readline().split())
s = stdin.readline().strip()
counts = [0 for i in range(n)]
count = 0
for i in range(1, n):
    if s[i-1:i+1] == 'AC':
        count += 1
    counts[i] = count

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

Submission status: Accepted

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

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

Submission status: Accepted

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

total = []
total.append(0)
for i in range(1, N):
    if S[i - 1] == 'A' and S[i] == 'C':
        total.append(1)
    else:
        total.append(0)
totsum = 0
for idx, val in enumerate(total):
    totsum += val
    total[idx] = totsum

for lr in lrs:
    print(total[lr[1] - 1] - total[lr[0] - 1])

Submission status: Accepted

Solution code:
N, Q = map(int, raw_input().split())
string = raw_input()
cnt = [0]
for i in range(N):
#    print(i)
#    print(string[i-2:i])
#    if i == 0:
#        cnt.append(0)
#        continue
    if string[i-1:i+1] == 'AC':
        cnt.append(cnt[i]+1)
        continue
    cnt.append(cnt[i])

for j in range(Q):
    #print(j)
    pare = map(int, raw_input().split())
    print(cnt[pare[1]] - cnt[pare[0]])

Submission status: Accepted

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

dp = [0 for column in range(n)]

for j in range(1, n):
    if s[j-1] == 'A' and s[j] == 'C':
        dp[j] = dp[j-1] + 1
    else:
        dp[j] = dp[j-1]

for _ in range(q):
    i, j =  map(int, input().split())
    print(dp[j-1] - dp[i-1])

Submission status: Accepted

Solution code:
n, q = map(int, raw_input().split())
s = str(raw_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, raw_input().split())
	print t[r-1] - t[l-1]

Submission status: Accepted

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

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

Submission status: Accepted

Solution code:
N,Q = map(int,input().split())
s = str(input())
data = [0]
cnt = 0
for i in range(1,N):
    if s[i-1] == 'A' and s[i] == 'C':
        cnt += 1
        data.append(cnt)
    else :
        data.append(cnt)
for i in range(Q):
    l,r = map(int,input().split())
    r -= 1
    l -= 1
    ans = data[r] - data[l]
    print(ans)

Submission status: Accepted

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

accum = [0 for _ in range(n + 1)]

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

for (l, r) in qs:
    print(accum[r] - accum[l])

Submission status: Accepted

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

li = [0]*N
for i in range(N-1):
    if S[i] == 'A' and S[i+1] == 'C':
        li[i+1] = 1
ans = list(itertools.accumulate(li))
k = []
for j in range(Q):
    a,b = [int(i) for i in input().split()]
    k.append(ans[b-1]-ans[a-1])

[print(i) for i in k]

Submission status: Accepted

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

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

data=[0]
count=0
for i in range(1,len(s)):
    if s[i-1]=="A" and s[i]=="C":
        count+=1
    data.append(count)

for l,r in lr:
    print(data[r-1]-data[l-1])

Submission status: Accepted

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

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

#print(x)
for l,r in lr:
    print(x[r-1] - x[l-1])

Submission status: Accepted

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


def solve(N: int, Q: int, S: str, l: "List[int]", r: "List[int]"):
    mem = [0 for i in range(N)]
    for i in range(1,len(S)):
        if S[i] == "C" and S[i - 1] == "A":
            mem[i] = mem[i-1]+1
        else:
            mem[i] = mem[i-1]
    for i in range(Q):
        start = l[i] - 1
        end = r[i] - 1
        print(mem[end] - mem[start])
    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: Accepted

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

n, q = map(int, input().split())
s = input()
ac_list = [0]

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

for _ in range(q):
  first, last = map(int, input().split())
  print(ac_list[last-1] - ac_list[first-1])

Submission status: Accepted

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


## No.5
# n番目までに何回ACって出るか調べたらO(N)で済むのでは？
c = 0
ac_list = []
for i in range(len(S)) :
    # Cの位置を基準にする
    if (S[i] == "C" and i > 0 and S[i-1] == "A") :
        c += 1
        ac_list.append(c)
    else :
        ac_list.append(c)

for item in q_list :
    # XXXXA | C だったケースは除外するので、右端は-1する
    ac_count = ac_list[item[1]-1] - ac_list[item[0]-1]
    print(ac_count)



## No.4
# a_indexes = []
# # 下処理として、Aの場所だけ覚えておく
# for i in range(len(S)) :
#     if (S[i] == "A") :
#         a_indexes.append(i)

# # 幅に応じて、Aの次がCかどうかを判定する
# for item in q_list :
#     from_index = item[0]
#     to_index = item[1]
#     # 次の文字がCであることを調べるので、toは1文字手前にする
#     target_indexes = [i for i in a_indexes if i >= from_index-1 and i < to_index-1]
#     ac_count = 0
#     for index in target_indexes :
#         if (S[index+1] == "C") :
#             ac_count += 1
    
#     print(ac_count)


## No.1  もちろんTLE。。。
# for item in q_list :
#     from_index = item[0]
#     to_index = item[1]
#     search_str = S[from_index-1:to_index]
#     find_index = 0
#     ac_count = 0
#     while(find_index != -1) :
#         find_index = search_str.find("AC",find_index)
#         if (find_index != -1) :
#             ac_count += 1
#             find_index += 1
#     print(ac_count)

## No.2,3
# for item in q_list :
#     from_index = item[0]
#     to_index = item[1]
#     search_str = S[from_index-1:to_index]
    # print(search_str.replace("AC","X").count("X"))
    # print(search_str.count("AC"))