Description
Submission
- O((n+q)lgn)
- May also solve the problem with hash map
#include <bits/stdc++.h>
using namespace std;
// Complete the matchingStrings function below.
vector<int> matchingStrings(vector<string> strings, vector<string> queries) {
vector<int> res;
sort(strings.begin(), strings.end());
for(string q : queries) {
auto lb = lower_bound(strings.begin(), strings.end(), q);
if(*lb != q) {
res.push_back(0);
continue;
}
int count = 0;
for(; *lb == q; advance(lb, 1)) {
count++;
}
res.push_back(count);
}
return res;
}
int main()
{
ofstream fout(getenv("OUTPUT_PATH"));
int strings_count;
cin >> strings_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<string> strings(strings_count);
for (int i = 0; i < strings_count; i++) {
string strings_item;
getline(cin, strings_item);
strings[i] = strings_item;
}
int queries_count;
cin >> queries_count;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
vector<string> queries(queries_count);
for (int i = 0; i < queries_count; i++) {
string queries_item;
getline(cin, queries_item);
queries[i] = queries_item;
}
vector<int> res = matchingStrings(strings, queries);
for (int i = 0; i < res.size(); i++) {
fout << res[i];
if (i != res.size() - 1) {
fout << "\n";
}
}
fout << "\n";
fout.close();
return 0;
}