-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHackerRank String Query
77 lines (75 loc) · 2.02 KB
/
HackerRank String Query
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <bits/stdc++.h>
using namespace std;
vector<int> compute(vector<string> &words, map<string, vector<int>> &Map) {
vector<int> Set1(Map[words[0]].begin(), Map[words[0]].end());
for(int i = 1; i < words.size(); i++) {
vector<int> Set2(Map[words[i]].begin(), Map[words[i]].end());
vector<int> Set3(Set1.size());
vector<int> Set4(Set1.size());
auto it = set_difference(Set1.begin(), Set1.end(), Set2.begin(), Set2.end(), Set3.begin());
it = set_difference(Set1.begin(), Set1.end(), Set3.begin(), it, Set4.begin());
Set1.clear();
for(auto iter = Set4.begin(); iter != it; iter++) {
Set1.push_back(*iter);
}
}
if(Set1.size() == 0) {
Set1.push_back(-1);
}
return Set1;
}
int main() {
// your code goes here
int n;
cin >> n;
string sentence[n];
map<string, vector<int>> Map;
getline(cin, sentence[0]);
for(int i = 0; i < n; i++) {
getline(cin, sentence[i]);
}
for(int i = 0; i < n; i++) {
sentence[i] += " ";
string word = "";
for(int j = 0; j < sentence[i].length(); j++) {
if(sentence[i][j] != ' ')
word += sentence[i][j];
else {
Map[word].push_back(i);
word = "";
}
}
}
int q;
cin >> q;
vector<vector<int>> ans(q, vector<int>());
string queries[q];
getline(cin, queries[0]);
for(int i = 0; i < q; i++) {
getline(cin, queries[i]);
}
for(int i = 0; i < q; i++) {
queries[i] += " ";
string word = "";
vector<string> words;
for(int j = 0; j < queries[i].length(); j++) {
if(queries[i][j] != ' ')
word += queries[i][j];
else {
words.push_back(word);
word = "";
}
}
vector<int> temp = compute(words, Map);
for(int j = 0; j < temp.size(); j++) {
ans[i].push_back(temp[j]);
}
}
for(int i = 0; i < ans.size(); i++) {
for(int j = 0; j < ans[i].size(); j++) {
cout << ans[i][j] << " ";
}
cout << endl;
}
return 0;
}