fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <array>
  4.  
  5. using namespace std;
  6.  
  7. char find(const string& s, int left, int right)
  8. {
  9. // declare an array for all 26 letters and initialize it with 0;
  10. // indexes are alphabet letters
  11. // values are letters appearances
  12. array<int, 26> t{};
  13.  
  14. // fill in the array with letter count between left & right indexes
  15. for (int i = left; i <= right; ++i)
  16. t[s[i] - 'a']++; // 'a' must be index 0, so we subtract it form current letter
  17.  
  18. // look for the maximum value the index plus 'a' is the letter - see the table comment
  19. int m = -1, idx = -1;
  20. for (size_t i = 0; i < t.size(); ++i)
  21. {
  22. if (t[i] > m)
  23. {
  24. m = t[i]; // we have a new maximum
  25. idx = i; // remember where the new maximum is
  26. }
  27. }
  28.  
  29. // the index starts from zero, so we add 'a'
  30. return idx + 'a';
  31. }
  32.  
  33.  
  34. int main()
  35. {
  36. //
  37. string s;
  38. if (!(cin >> s))
  39. return -1; // bad input
  40.  
  41. //
  42. int query_count;
  43. if (!(cin >> query_count))
  44. return -2;
  45.  
  46. //
  47. for (int i = 0; i < query_count; ++i)
  48. {
  49. int left, right;
  50. if (!(cin >> left >> right))
  51. return -3;
  52. cout << find(s, left, right) << endl;
  53. }
  54.  
  55. return 0;
  56. }
Success #stdin #stdout 0.01s 5460KB
stdin
badbadbed
4
0 8
1 4
0 5
2 7
stdout
b
a
a
b