fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. class Solution {
  5. public:
  6. vector<string> words;
  7.  
  8. Solution(vector<string>& words) : words(words) {
  9. }
  10.  
  11. int f(const string &prefix, const string &suffix) {
  12. int idx = -1;
  13. for (int i = 0; i < words.size(); i++) {
  14. if (words[i].substr(0, prefix.size()) == prefix && words[i].substr(words[i].size() - suffix.size()) == suffix) {
  15. idx = i;
  16. }
  17. }
  18. return idx;
  19. }
  20. };
  21.  
  22.  
  23. int main() {
  24.  
  25.  
  26. vector<string> v = {"aae", "apple", "banana"};
  27. Solution solution(v);
  28.  
  29. vector<pair<string, string>> queries = {{"a", "e"}, {"a", "x"}, {"a", "ae"}};
  30.  
  31. for (const auto& p : queries) {
  32. int result = solution.f(p.first, p.second);
  33. cout << result << endl;
  34. }
  35.  
  36. return 0;
  37. }
  38.  
Success #stdin #stdout 0s 5292KB
stdin
Standard input is empty
stdout
1
-1
0