fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int main() {
  5. int n,q; cin >> n >> q;
  6. vector <string> v;
  7. vector <vector <int> > pref(n+1, vector <int>(26,0)); // pref[i][j] -> it means that cumulative frquency of characters from 'a' to 'z' is being stored till ith index
  8. for(int i = 0; i < n; i++) {
  9. string s; cin >> s;
  10. v.push_back(s);
  11.  
  12. for(int j = 0; j < s.length(); j++) {
  13. pref[i+1][s[j]-'a']++;
  14. }
  15. }
  16.  
  17. for(int i = 2; i < n+1; i++) {
  18. for(int j = 0; j < 26; j++) pref[i][j] += pref[i-1][j];
  19. }
  20.  
  21. while(q--) {
  22. int l,r,k; cin >> l >> r >> k;
  23. int cnt = 0;
  24. char op;
  25. for(int i = 0; i < 26; i++) {
  26. cnt += pref[r][i] - pref[l-1][i];
  27. if(cnt >= k) {
  28. op = i+97;
  29. break;
  30. }
  31. }
  32. cout << op << "\n";
  33. }
  34.  
  35. }
Success #stdin #stdout 0s 5672KB
stdin
3 2
abcc
trea 
zape
2 3 5
2 2 1
stdout
p
a