Skip to content

Commit feaa970

Browse files
committed
AC P867 P1178
1 parent 006cd61 commit feaa970

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

Diff for: P1178.cpp

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#include "header.h"
2+
#include <unordered_map>
3+
#include <vector>
4+
5+
class Solution {
6+
private:
7+
inline int getHash(const string &s) {
8+
int h=0;
9+
for (const auto &c:s) {
10+
h |= 1 << (c - 'a');
11+
}
12+
return h;
13+
}
14+
public:
15+
vector<int> findNumOfValidWords(vector<string>& words, vector<string>& puzzles) {
16+
unordered_map<int, int> words_hash;
17+
for (const auto &w:words) {
18+
int h = getHash(w);
19+
words_hash[h]++;
20+
}
21+
vector<int> ans(puzzles.size(), 0);
22+
for (int j=0; j<puzzles.size(); j++) {
23+
int &&puzzle_hash = getHash(puzzles[j]);
24+
int &&first = 1<<(puzzles[j][0]-'a');
25+
int k=0;
26+
for (int i=puzzle_hash; i; i=(i-1)&puzzle_hash)
27+
if ((i&first)!=0) ans[j] += words_hash[i];
28+
}
29+
return ans;
30+
}
31+
};
32+
33+
int main() {
34+
vector<string> words, puzzles;
35+
36+
words = {"aaaa","asas","able","ability","actt","actor","access"};
37+
puzzles = {"aboveyz","abrodyz","abslute","absoryz","actresz","gaswxyz"};
38+
print(Solution().findNumOfValidWords(words, puzzles));
39+
return 0;
40+
}

Diff for: P867.cpp

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#include "header.h"
2+
#include <vector>
3+
4+
class Solution {
5+
public:
6+
vector<vector<int>> transpose(vector<vector<int>>& matrix) {
7+
int rows,cols;
8+
if (matrix.empty()) return {};
9+
rows = matrix.size();
10+
cols = matrix.front().size();
11+
vector<vector<int>> ret;
12+
ret.resize(cols);
13+
for (auto &c:ret) c.resize(rows);
14+
for (int i=0; i<rows; i++) {
15+
for (int j=0; j<cols; j++) {
16+
ret[j][i] = matrix[i][j];
17+
}
18+
}
19+
return ret;;
20+
}
21+
};
22+
23+
int main() {
24+
return 0;
25+
}

0 commit comments

Comments
 (0)