#include <bits/stdc++.h>
using namespace std;
class Solution {
public:
vector<string> words;
Solution(vector<string>& words) : words(words) {
}
int f(const string &prefix, const string &suffix) {
int idx = -1;
for (int i = 0; i < words.size(); i++) {
if (words[i].substr(0, prefix.size()) == prefix && words[i].substr(words[i].size() - suffix.size()) == suffix) {
idx = i;
}
}
return idx;
}
};
int main() {
vector<string> v = {"aae", "apple", "banana"};
Solution solution(v);
vector<pair<string, string>> queries = {{"a", "e"}, {"a", "x"}, {"a", "ae"}};
for (const auto& p : queries) {
int result = solution.f(p.first, p.second);
cout << result << endl;
}
return 0;
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CgpjbGFzcyBTb2x1dGlvbiB7CnB1YmxpYzoKICAgIHZlY3RvcjxzdHJpbmc+IHdvcmRzOwogICAgCiAgICBTb2x1dGlvbih2ZWN0b3I8c3RyaW5nPiYgd29yZHMpIDogd29yZHMod29yZHMpIHsKICAgIH0KICAgIAogICAgaW50IGYoY29uc3Qgc3RyaW5nICZwcmVmaXgsIGNvbnN0IHN0cmluZyAmc3VmZml4KSB7CiAgICAgICAgaW50IGlkeCA9IC0xOwogICAgICAgIGZvciAoaW50IGkgPSAwOyBpIDwgd29yZHMuc2l6ZSgpOyBpKyspIHsKICAgICAgICAgICAgaWYgKHdvcmRzW2ldLnN1YnN0cigwLCBwcmVmaXguc2l6ZSgpKSA9PSBwcmVmaXggJiYgd29yZHNbaV0uc3Vic3RyKHdvcmRzW2ldLnNpemUoKSAtIHN1ZmZpeC5zaXplKCkpID09IHN1ZmZpeCkgewogICAgICAgICAgICAgICAgaWR4ID0gaTsKICAgICAgICAgICAgfQogICAgICAgIH0KICAgICAgICByZXR1cm4gaWR4OwogICAgfQp9OwoKCmludCBtYWluKCkgewogICAKCiAgICB2ZWN0b3I8c3RyaW5nPiB2ID0geyJhYWUiLCAiYXBwbGUiLCAiYmFuYW5hIn07CiAgICBTb2x1dGlvbiBzb2x1dGlvbih2KTsKCiAgICB2ZWN0b3I8cGFpcjxzdHJpbmcsIHN0cmluZz4+IHF1ZXJpZXMgPSB7eyJhIiwgImUifSwgeyJhIiwgIngifSwgeyJhIiwgImFlIn19OwoKICAgIGZvciAoY29uc3QgYXV0byYgcCA6IHF1ZXJpZXMpIHsKICAgICAgICBpbnQgcmVzdWx0ID0gc29sdXRpb24uZihwLmZpcnN0LCBwLnNlY29uZCk7CiAgICAgICAgY291dCA8PCByZXN1bHQgPDwgZW5kbDsKICAgIH0KCiAgICByZXR1cm4gMDsKfQo=