-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsolution.cpp
executable file
·62 lines (49 loc) · 1.24 KB
/
solution.cpp
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
#include <algorithm>
#include <iostream>
#include <string>
#include <unordered_map>
#include <vector>
using namespace std;
#define ALPHABET_SIZE 26
string calculateHashString(string word) {
vector<int> alphabet(ALPHABET_SIZE);
for (char ch : word) {
alphabet[tolower(ch) - 'a']++;
}
vector<int> letterCounts;
for (int i = 0; i < ALPHABET_SIZE; i++) {
if (alphabet[i] > 0)
letterCounts.push_back(alphabet[i]);
}
sort(letterCounts.begin(), letterCounts.end());
string hashString;
for (int count : letterCounts) {
hashString += to_string(count);
hashString += ',';
}
return hashString;
}
void printVector(vector<int>& v) {
for (int i = 0; i < v.size(); i++) {
cout << v[i] << " ";
}
cout << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int n, m;
cin >> n >> m;
unordered_map<string, vector<int>> acronyms;
for (int i = 0; i < n; i++) {
string word;
cin >> word;
acronyms[calculateHashString(word)].push_back(i);
}
for (int i = 0; i < m; i++) {
string query;
cin >> query;
printVector(acronyms[calculateHashString(query)]);
}
return 0;
}