Problem p03087 (C++)

Buggy submission (status: Time Limit Exceeded):
// https://atcoder.jp/contests/abc122/tasks/abc122_c

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <set>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

#define EPS (1e-7)
#define INF (1e9)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

// long gcd(long a, long b) { return b ? gcd(b, a % b) : a; }
// long lcm(long a, long b) { return a * b / gcd(a, b); }

vector<string> search(string const &text, regex const &re) {
  vector<string> result;

  auto iter = text.cbegin();
  auto end = text.cend();

  smatch m;

  while (regex_search(iter, end, m, re)) {
    result.push_back(m.str());
    iter = m[0].second;
  }

  return result;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  int l[q], r[q];
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
  }

  regex re("AC");
  // n文字目から末尾までのAC出現回数
  int findAC[n];
  for (int i = 0; i < n; i++) {
    string tmp = s.substr(i, s.length() - i);
    auto result = search(tmp, re);
    findAC[i] = result.size();
  }

  for (int i = 0; i < q; i++) {
    cout << findAC[l[i] - 1] - findAC[r[i] - 1] << '\n';
  }

  return 0;
}


Fixed submission:
// https://atcoder.jp/contests/abc122/tasks/abc122_c

#include <algorithm>
#include <array>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <regex>
#include <set>
#include <tuple>
#include <vector>
using namespace std;

typedef long long ll;
typedef pair<int, int> PII;
typedef pair<ll, ll> PLL;

#define EPS (1e-7)
#define INF (1e9)
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) for (int i = 0; i < (n); ++i)
#define REPR(i, n) for (int i = n; i >= 0; i--)
#define FOREACH(x, a) for (auto &(x) : (a))
#define ALL(obj) (obj).begin(), (obj).end()
#define ALLR(obj) (obj).rbegin(), (obj).rend()

// long gcd(long a, long b) { return b ? gcd(b, a % b) : a; }
// long lcm(long a, long b) { return a * b / gcd(a, b); }

vector<string> search(string const &text, regex const &re) {
  vector<string> result;

  auto iter = text.cbegin();
  auto end = text.cend();

  smatch m;

  while (regex_search(iter, end, m, re)) {
    result.push_back(m.str());
    iter = m[0].second;
  }

  return result;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, q;
  cin >> n >> q;
  string s;
  cin >> s;
  int l[q], r[q];
  for (int i = 0; i < q; i++) {
    cin >> l[i] >> r[i];
  }

  regex re("AC");
  // n文字目から末尾までのAC出現回数
  int findAC[n];
  int cnt = 0;
  findAC[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    if (s[i] == 'A' && s[i + 1] == 'C') {
      cnt++;
    }
    findAC[i] = cnt;
  }

  for (int i = 0; i < q; i++) {
    cout << findAC[l[i] - 1] - findAC[r[i] - 1] << '\n';
  }

  return 0;
}