Cod sursa(job #2924059)

Utilizator AztecaEdward Andrew Nedelcu Azteca Data 23 septembrie 2022 21:53:13
Problema Perb Scor Ascuns
Compilator cpp-64 Status waiting
Runda Arhiva de probleme Marime 1.63 kb
#include <bits/stdc++.h>

using ll = long long;

const int MAX_N = 600;
const int SIGMA = 257;

int dp[MAX_N + 1][MAX_N + 1][5], answer[MAX_N + 1][MAX_N + 1];
int index[SIGMA];
std::vector<int> divs[MAX_N + 1];

void precompute(std::string s) {
  int n = s.size();
  index['A'] = 1;
  index['C'] = 2;
  index['T'] = 3;
  index['G'] = 4;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      for (int letter = 1; letter <= 4; letter++) {
        if (i - j > 0) {
          dp[i][j][letter] = dp[i - j][j][letter];
        }
        if (index[s[i - 1]] != letter) {
          dp[i][j][letter]++;
        }
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 2 * i; j <= n; j += i) {
      divs[j].push_back(i);
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = i; j <= n; j++) {
      int h = j - i + 1;
      answer[i][j] = n;
      for (int d : divs[h]) {
        int sum = 0;
        for (int k = 0; k < d; k++) {
          int mn = n;
          for (int letter = 1; letter <= 4; letter++) {
            int value = dp[j - k][d][letter];
            if (i - k > 1) {
              value -= dp[i - k - 1][d][letter];
            }
            mn = std::min(mn, value);
          }
          sum += mn;
        }
        answer[i][j] = std::min(answer[i][j], sum);
      }
    }
  }
}

int main() {
  std::ifstream fin("perb.in");
  std::ofstream fout("perb.out");
  int n, m;
  std::string s;
  fin >> n >> m >> s;
  precompute(s);
  for (int i = 1; i <= m; i++) {
    int l, r;
    fin >> l >> r;
    fout << answer[l][r] << "\n";
  }
  return 0;
}