-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
804.cpp
57 lines (54 loc) · 1.88 KB
/
804.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
__________________________________________________________________________________________________
sample 4 ms submission
#include <unordered_set>
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
unordered_set<string> morses{};
for (const auto& i : words) {
string morse{};
for (auto j : i) {
morse += MORSES[j - 'a'];
}
morses.emplace(move(morse));
}
return morses.size();
}
private:
const static vector<string> MORSES;
};
const vector<string> Solution::MORSES{
".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."
};
__________________________________________________________________________________________________
sample 9116 kb submission
class Solution {
public:
int uniqueMorseRepresentations(vector<string>& words) {
map<long long, int> tbl;
char * scode[] = {".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--.."};
int code[26];
int len[26];
for (int i=0, j; i<26; i++)
{
code[i] = 0;
for (j=0; scode[i][j]; j++)
{
code[i] = (code[i] << 1) + (scode[i][j] == '.' ? 1 : 0);
}
len[i] = j;
}
for (int i=0; i<words.size(); i++)
{
long long res = 0;
string s = words[i];
for (int j=0; j<s.size(); j++)
{
res = (res << len[s[j]-'a']) + code[s[j]-'a'];
}
tbl[res] = 1;
}
return tbl.size();
}
};
__________________________________________________________________________________________________