fork(3) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. const int N = 1e4;
  5. int n, q, cum[N][26];
  6. char s[N + 1];
  7.  
  8. inline long long calc(int k, int ch) {
  9. long long res = 1LL * k / n * cum[n - 1][ch];
  10. if (k % n != 0)
  11. res += cum[k % n - 1][ch];
  12. return res;
  13. }
  14.  
  15. int main(int argc, char **argv) {
  16. int t;
  17. scanf("%d", &t);
  18. while (t-- != 0) {
  19. scanf("%d%d%s", &n, &q, s);
  20. memset(cum[0], 0, sizeof cum[0]);
  21. ++cum[0][s[0] - 'a'];
  22. for (int i = 1; i < n; ++i) {
  23. for (int j = 0; j < 26; ++j)
  24. cum[i][j] = cum[i - 1][j];
  25. ++cum[i][s[i] - 'a'];
  26. }
  27. int l, r;
  28. char c;
  29. while (q-- != 0) {
  30. scanf("%d%d %c", &l, &r, &c);
  31. printf("%lld\n", calc(r, c - 'a') - calc(l - 1, c - 'a'));
  32. }
  33. }
  34. return 0;
  35. }
Runtime error #stdin #stdout 0s 16264KB
stdin
Standard input is empty
stdout
Standard output is empty