Submission #942734

# Submission time Handle Problem Language Result Execution time Memory
942734 2024-03-11T03:40:35 Z artixkrishna Mate (COCI18_mate) C++14
80 / 100
213 ms 68084 KB
#include <bits/stdc++.h>
using namespace std;
#define mod (1000000000 + 7)
#define N 2005
typedef long long ll;

string s;
int q, n, qtd[30][N];
ll dp[N][30][30], ch[N][N];

vector<int> pos[30];

ll choose(int a, int b) {
  if (a == b || b == 0)
    return 1;
  if (b == 1)
    return a;
  if (b > a)
    return 0;
  if (ch[a][b] != -1)
    return ch[a][b];
  return ch[a][b] = (choose(a - 1, b - 1) + choose(a - 1, b)) % mod;
}
void solve(int a, int b) {
  for (int len = 1; len <= n; len++) {
    for (int i = 0; i < pos[a].size(); i++) {
      int x = pos[a][i];
      dp[len][a][b] += ((ll)qtd[b][x + 1] * choose(x - 1, len - 2)) % mod;
      dp[len][a][b] %= mod;
    }
  }
}

int main() {
  cin >> s;
  n = s.size();
  for (int c = 0; c < 30; c++) {
    for (int i = n; i >= 1; i--) {
      qtd[c][i] = qtd[c][i + 1] + (s[i - 1] - 'a' == c);
    }
  }
  for (int i = 1; i <= n; i++)
    pos[s[i - 1] - 'a'].push_back(i);
  memset(ch, -1, sizeof(ch));
  for (int a = 0; a < 30; a++) {
    for (int b = 0; b < 30; b++) {
      solve(a, b);
    }
  }
  cin >> q;
  while (q--) {
    int l;
    string aux;
    cin >> l >> aux;
    int a = aux[0] - 'a', b = aux[1] - 'a';
    cout << dp[l][a][b] << endl;
  }
}

Compilation message

mate.cpp: In function 'void solve(int, int)':
mate.cpp:26:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   26 |     for (int i = 0; i < pos[a].size(); i++) {
      |                     ~~^~~~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 43 ms 33628 KB Output is correct
2 Correct 29 ms 33628 KB Output is correct
3 Correct 34 ms 33696 KB Output is correct
4 Correct 54 ms 33824 KB Output is correct
5 Correct 189 ms 35152 KB Output is correct
6 Correct 213 ms 35508 KB Output is correct
7 Correct 168 ms 35156 KB Output is correct
8 Correct 149 ms 34936 KB Output is correct
9 Runtime error 41 ms 68084 KB Execution killed with signal 11
10 Runtime error 39 ms 67924 KB Execution killed with signal 11