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.

Solution in Java8 (OpenJDK 1.8.0) (judge verdict: Time Limit Exceeded)


import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;

public class Main {

	public static void main(String[] args) throws IOException {
		new Main().solve();
	}

	private void solve() throws IOException {
		try {
			//			solveA();
			//			solveB();
			solveC();
			// solveD();
			// solveE();
			// solveF();
		} finally {
			if (in != null) {
				in.close();
			}
			if (out != null) {
				out.flush();
				out.close();
			}
		}

	}

	private void solveA() {
		String wk = next();
		if (wk.equals("A")) {
			System.out.println("T");
		} else if (wk.equals("T")) {
			System.out.println("A");
		} else if (wk.equals("G")) {
			System.out.println("C");
		} else if (wk.equals("C")) {
			System.out.println("G");
		}

	}

	private int getLength(String wk, int currentI, int total) {
		if (currentI >= wk.length()) {
			return total;
		}
		String wkS = wk.substring(currentI, currentI + 1);
		if (wkS.equals("A") || wkS.equals("T") || wkS.equals("G") || wkS.equals("C")) {
			return getLength(wk, currentI + 1, total) + 1;
		} else {
			return total;
		}
	}

	private void solveB() {
		String s = next();
		int res = 0;
		for (int i = 0; i < s.length(); i++) {
			int wkRes = getLength(s, i, 0);
			res = Math.max(wkRes, res);
		}

		out.println(res);
	}

	private int getLengthC(String wk, int currentI, int total) {
		if (currentI >= wk.length() - 1) {
			return total;
		}
		String wkS = wk.substring(currentI, currentI + 2);
		if (wkS.equals("AC")) {
			return getLengthC(wk, currentI + 2, total) + 1;
		} else {
			return getLengthC(wk, currentI + 1, total);
		}
	}

	Pattern p = Pattern.compile("AC");

	private static String replaceByHand(final String original, final String old, final String replace) {
		int offset = 0;
		StringBuilder builder = new StringBuilder(original.length());
		int index;
		while (0 <= (index = original.indexOf(old, offset))) {
			builder.append(original, offset, index);
			builder.append(replace);
			offset = index + old.length();
		}
		builder.append(original, offset, original.length());
		return builder.toString();
	}

	private int getLengthC2(String wk, int start, int last) {
		int total = 0;
		wk = wk.substring(start, last);

		/**
		 * p7
		 */
		total = (wk.length() - replaceByHand(wk, "AC", "").length()) / 2;

		/**
		 * p6
		 */
		//		String[] wkL = wk.split("");
		//		for (int i = start + 1; i < last;) {
		//			if (wkL[i].equals("C") && wkL[i - 1].equals("A")) {
		//				total++;
		//				i += 2;
		//			} else {
		//				i++;
		//			}
		//		}

		/**
		 * p5
		 */
		//		total = (wk.length() - wk.replaceAll("AC", "").length()) / 2;

		/**
		 * 4
		 */
		//		Matcher m = p.matcher(wk);
		//		int s = 0;
		//		while (m.find(s)) {
		//			total++;
		//			s = m.end();
		//		}

		/**
		 * p2
		 */
		//		if (wk.lastIndexOf("AC") == 3) {
		//			total = wk.split("AC").length;
		//		} else {
		//			total = wk.split("AC").length - 1;
		//		}

		/**
		 * p1
		 */
		//		total = (wk.length() - wk.replace("AC", "").length()) / 2;

		/**
		 * p1
		 */
		//		int num = 0;
		//		while (num != -1) {
		//			num = wk.indexOf("AC");
		//			if (num != -1) {
		//				total++;
		//				wk = wk.substring(2, wk.length());
		//			}
		//		}
		return total;
	}

	private void solveC() {
		int numN = nextInt();
		int numQ = nextInt();
		String strS = next();
		int[][] wk = new int[numQ][2];

		for (int i = 0; i < wk.length; i++) {
			wk[i][0] = nextInt();
			wk[i][1] = nextInt();
		}

		for (int[] is : wk) {
			//			String wkS = strS.substring(is[0] - 1, is[1]);
			int res = 0;
			//			for (int i = 0; i < wkS.length(); i++) {
			//				String firstS = wkS.substring(i, i + 1);
			//				boolean isNextA = firstS.equals("A");
			//				int wkRes = 0;
			//				if (isNextA) {
			//					wkRes = getLengthC(wkS, i, 0);
			//				}
			//				res = Math.max(wkRes, res);
			//			}
			res = getLengthC2(strS, is[0] - 1, is[1]);
			System.out.println(res);
		}

	}

	private void solveD() {
		int numN = nextInt();

		out.println("");
	}

	private void solveE() {
		int numN = nextInt();

		out.println("");
	}

	private void solveF() {
		int numN = nextInt();

		out.println("");
	}

	private final PrintWriter out = new PrintWriter(System.out);
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;

	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}

	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}

	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}

	private void skipUnprintable() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
	}

	public boolean hasNext() {
		skipUnprintable();
		return hasNextByte();
	}

	public int nextInt() {
		return Integer.parseInt(next());
	}

	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}

	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
}