-
Notifications
You must be signed in to change notification settings - Fork 0
/
06.AlienDict.cpp
101 lines (84 loc) · 2.64 KB
/
06.AlienDict.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
Description
There is a new alien language which uses the latin alphabet. However, the order among letters are unknown to you. You receive a list of non-empty words
from the dictionary, where words are sorted lexicographically by the rules of this new language. Derive the order of letters in this language.
You may assume all letters are in lowercase.
You may assume that if a is a prefix of b, then a must appear before b in the given dictionary.
If the order is invalid, return an empty string.
There may be multiple valid order of letters, return the smallest in lexicographical order
Example
Given the following words in dictionary,
[
"wrt",
"wrf",
"er",
"ett",
"rftt"
]
The correct order is: "wertf"
Given the following words in dictionary,
[
"z",
"x"
]
The correct order is: "zx".
*/
// from => to means from is smaller than to
#include "LeetcodeTools.h"
using namespace leetcode;
class Solution{
public:
string alienOrder(vector<string> &words) {
buildGraph(words);
priority_queue<int, vector<int>, std::greater<int>> zeroDegNodes;
int total = 0;
for(int i = 0; i < words.size(); i++) {
for(int j = 0; j < words[i].size(); j++) {
if(occured[words[i][j] - 'a'] == false) {
occured[words[i][j] - 'a'] = true;
total++;
}
}
}
for(int i = 0; i < 26; i++) if(occured[i] && inDeg[i] == 0) zeroDegNodes.push(i);
string res = "";
while(!zeroDegNodes.empty()) {
int cur = zeroDegNodes.top(); zeroDegNodes.pop();
for(int i = 0; i < G[cur].size(); i++){
if(--inDeg[G[cur][i]] == 0) zeroDegNodes.push(G[cur][i]);
}
res.push_back('a' + cur);
}
return (res.size() < total ) ? "" : res;
}
private:
vector<int> G[26];
int inDeg[26]; //入度
bool occured[26];
void buildGraph(vector<string> & words) {
memset(inDeg, 0, sizeof(inDeg));
memset(occured, false, sizeof(occured));
for(int i = 0; i < words.size() - 1; i++) {
int j = 0;
while(j < words[i].size() && words[i][j] == words[i + 1][j]) j++;
if(j == words[i].size()) continue;
int from = words[i][j] - 'a';
int to = words[i + 1][j] - 'a';
G[from].push_back(to);
inDeg[to]++;
}
}
};
int main () {
Solution sol;
vector<string> words{
"wrt",
"wrf",
"er",
"ett",
"rftt"
};
//vector<string> words {"ab", "adc"};
cout << sol.alienOrder(words) << endl;
return 0;
}