-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path45-alien-dictionary.cpp
117 lines (99 loc) · 2.69 KB
/
45-alien-dictionary.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Initial Template for C++
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
// User function Template for C++
class Solution{
public:
void DFSRec(unordered_set<int> adj[], int u, string &ans, vector<bool> &visited)
{
visited[u] = true;
for(auto v: adj[u])
{
if(visited[v] == false)
{
DFSRec(adj, v, ans, visited);
}
}
ans.push_back(u+'a');
}
string topologicalSort(unordered_set<int> adj[], int V)
{
string ans;
vector<bool> visited(V, false);
for(int v=0; v < V; v++)
{
if(adj[v].size())
{
for(auto u: adj[v])
{
if(visited[u] == false)
{
DFSRec(adj, u, ans, visited);
}
}
}
}
reverse(ans.begin(),ans.end());
return ans;
}
string findOrder(string words[], int N, int K)
{
unordered_set<int> adj[K];
for(int i = 0; i < N-1; i++)
{
string word1 = words[i];
string word2 = words[i+1];
for(int j = 0; j < min(word1.size(), word2.size()); j++)
{
if(word1[j] != word2[j])
{
int index1 = word1[j] - 'a';
int index2 = word2[j] - 'a';
adj[index1].insert(index2);
break;
}
}
}
return topologicalSort(adj,K);
}
};
// { Driver Code Starts.
string order;
bool f(string a, string b) {
int p1 = 0;
int p2 = 0;
for (int i = 0; i < min(a.size(), b.size()) and p1 == p2; i++) {
p1 = order.find(a[i]);
p2 = order.find(b[i]);
// cout<<p1<<" "<<p2<<endl;
}
if (p1 == p2 and a.size() != b.size()) return a.size() < b.size();
return p1 < p2;
}
// Driver program to test above functions
int main() {
int t;
cin >> t;
while (t--) {
int N, K;
cin >> N >> K;
string dict[N];
for (int i = 0; i < N; i++) cin >> dict[i];
Solution obj;
string ans = obj.findOrder(dict, N, K);
order = "";
for (int i = 0; i < ans.size(); i++) order += ans[i];
string temp[N];
std::copy(dict, dict + N, temp);
sort(temp, temp + N, f);
bool f = true;
for (int i = 0; i < N; i++)
if (dict[i] != temp[i]) f = false;
if(f)cout << 1;
else cout << 0;
cout << endl;
}
return 0;
}
// } Driver Code Ends