|
1 | 1 | class Solution { |
| 2 | + int m; |
2 | 3 | public: |
3 | 4 | string minAbbreviation(string target, vector<string>& dictionary) |
4 | 5 | { |
5 | 6 | if (dictionary.size()==0) |
6 | | - return to_string(target.size()); |
7 | | - |
8 | | - unordered_set<string>Set; |
9 | | - int N = target.size(); |
| 7 | + return to_string(target.size()); |
| 8 | + this->m = target.size(); |
10 | 9 |
|
11 | | - for (auto str:dictionary) |
| 10 | + unordered_set<string>Set; |
| 11 | + for (auto word: dictionary) |
12 | 12 | { |
13 | | - if (str.size()==N) |
14 | | - Set.insert(str); |
15 | | - } |
16 | | - |
17 | | - vector<pair<int,int>>masks; |
18 | | - for (int i=0; i<(1<<N); i++) |
19 | | - masks.push_back({len(i,N),i}); |
20 | | - sort(masks.begin(),masks.end()); |
| 13 | + if (word.size()==m) |
| 14 | + Set.insert(word); |
| 15 | + } |
| 16 | + |
| 17 | + vector<pair<int,int>>masks; |
| 18 | + for (int state = 0; state < (1<<m); state++) |
| 19 | + masks.push_back({len(state), state}); |
| 20 | + sort(masks.begin(), masks.end()); |
21 | 21 |
|
22 | | - for (int i=0; i<masks.size(); i++) |
| 22 | + for (auto x: masks) |
23 | 23 | { |
24 | | - int mask = masks[i].second; |
25 | | - string a = abbr(target,mask); |
| 24 | + int mask = x.second; |
| 25 | + string abbr = getAbbr(target, mask); |
26 | 26 | int flag = 1; |
27 | | - |
28 | | - for (auto word:Set) |
| 27 | + |
| 28 | + for (auto word: Set) |
29 | 29 | { |
30 | | - string b = abbr(word, mask); |
31 | | - if (a==b) |
| 30 | + string s = getAbbr(word, mask); |
| 31 | + if (s==abbr) |
32 | 32 | { |
33 | 33 | flag = 0; |
34 | 34 | break; |
35 | | - } |
36 | | - } |
37 | | - if (flag == 1) return a; |
| 35 | + } |
| 36 | + } |
| 37 | + if (flag==1) return abbr; |
38 | 38 | } |
39 | | - return ""; |
| 39 | + |
| 40 | + return ""; |
40 | 41 | } |
41 | 42 |
|
42 | | - |
43 | | - int len(int mask, int N) |
| 43 | + string getAbbr(string& word, int mask) |
44 | 44 | { |
45 | | - int count = 0; |
46 | | - for (int i=0; i<N; i++) |
| 45 | + string ret; |
| 46 | + for (int i=0; i<m; i++) |
47 | 47 | { |
48 | | - if (((mask>>i)&1)==1) |
49 | | - count++; |
| 48 | + if ((mask>>i)&1) |
| 49 | + ret.push_back(word[i]); |
50 | 50 | else |
51 | 51 | { |
52 | | - int j = i+1; |
53 | | - while (j<N && ((mask>>j)&1)==0) |
| 52 | + int j = i; |
| 53 | + while (j<m && ((mask>>j)&1)==0) |
54 | 54 | j++; |
55 | | - count++; |
| 55 | + ret += to_string(j-i); |
56 | 56 | i = j-1; |
57 | 57 | } |
58 | | - } |
59 | | - return count; |
| 58 | + } |
| 59 | + return ret; |
60 | 60 | } |
61 | 61 |
|
62 | | - string abbr(string A, int mask) |
63 | | - { |
64 | | - string result; |
65 | | - int N = A.size(); |
66 | | - |
67 | | - for (int i=0; i<N; i++) |
| 62 | + int len(int mask) |
| 63 | + { |
| 64 | + int count = 0; |
| 65 | + for (int i=0; i<m; i++) |
68 | 66 | { |
69 | | - if (((mask>>i)&1)==1) |
70 | | - result.push_back(A[i]); |
| 67 | + if ((mask>>i)&1) |
| 68 | + count++; |
71 | 69 | else |
72 | 70 | { |
73 | | - int j = i+1; |
74 | | - while (j<N && ((mask>>j)&1)==0) |
| 71 | + int j = i; |
| 72 | + while (j<m && ((mask>>j)&1)==0) |
75 | 73 | j++; |
76 | | - result += to_string(j-i); |
| 74 | + count += to_string(j-i).size(); |
77 | 75 | i = j-1; |
78 | 76 | } |
79 | 77 | } |
80 | | - return result; |
| 78 | + return count; |
| 79 | + |
81 | 80 | } |
82 | | - |
83 | 81 | }; |
0 commit comments