From 53684a0c6b39c4e2a3ae2d40ecbcad2dac63183b Mon Sep 17 00:00:00 2001 From: Argha Sen Date: Sun, 28 Oct 2018 13:20:26 +0530 Subject: [PATCH] find alphabets from alien dictionary --- alphabets.cpp | 162 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 alphabets.cpp diff --git a/alphabets.cpp b/alphabets.cpp new file mode 100644 index 000000000..3b843599d --- /dev/null +++ b/alphabets.cpp @@ -0,0 +1,162 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using namespace std; + +using Vertex = char; +using Vertices = set; +using AdjList = unordered_map; + +class Graph +{ + private: + int numVertices; + AdjList adjList; + Vertices vertices; + + public: + Graph(Vertices vertices); + int getNumVertices() const; + const Vertices &getVertices() const; + void addEdge(Vertex from, Vertex to); + void printAdjList(); + const Vertices &getNeighbours(Vertex vertex) const; +}; + +Graph::Graph(Vertices vertices) + : vertices(vertices) +{ + numVertices = vertices.size(); + for (auto vertex : vertices) + { + adjList.insert(make_pair(vertex, set())); + } +} + +int Graph::getNumVertices() const +{ + return numVertices; +} + +const Vertices &Graph::getVertices() const +{ + return vertices; +} +void Graph::addEdge(Vertex from, Vertex to) +{ + adjList[from].insert(to); +} + +void Graph::printAdjList() +{ + cout << "AdjList:" << endl; + for (auto i : adjList) + { + cout << i.first << " -> [ "; + for (auto j : i.second) + cout << j << " "; + cout << " ]" << endl; + } +} + +const Vertices &Graph::getNeighbours(Vertex vertex) const +{ + auto res = adjList.find(vertex); + return res->second; +} + +void visitor(const Graph &graph, Vertex vertex, unordered_map &visited, stack &s) +{ + visited[vertex] = true; + auto neighbours = graph.getNeighbours(vertex); + + for (auto neighbour : neighbours) + { + auto visitItr = visited.find(neighbour); + if (visitItr == visited.end() || visitItr->second == false) + visitor(graph, neighbour, visited, s); + } + s.push(vertex); +} + +void topologicalSort(const Graph &graph) +{ + stack s; + int numVertices = graph.getNumVertices(); + auto vertices = graph.getVertices(); + unordered_map visited; + for (auto vertex : vertices) + { + auto visitItr = visited.find(vertex); + if (visitItr == visited.end() || visitItr->second == false) + visitor(graph, vertex, visited, s); + } + cout << "Sort Order:" << endl; + while (s.empty() == false) + { + cout << s.top() << " "; + s.pop(); + } +} + +set getAlphabets(vector dict) +{ + set alphabets; + for (auto &word : dict) + { + for (int i = 0; i < word.size(); i++) + { + alphabets.insert(word[i]); + } + } + return alphabets; +} +void createGraph(Graph &g, vector dict) +{ + string w1, w2; + int len; + for (int i = 0; i < dict.size() - 1; i++) + { + w1 = dict[i]; + w2 = dict[i + 1]; + len = min(w1.size(), w2.size()); + for (int j = 0; j < len; j++) + { + if (w1[j] != w2[j]) + { + g.addEdge(w1[j], w2[j]); + break; + } + } + } +} + +int main() +{ +#ifndef ONLINE_JUDGE + freopen("input.txt", "r", stdin); // redirects standard input + freopen("output.txt", "w", stdout); // redirects standard output +#endif + vector dict; + string a; + while (cin >> a) + dict.push_back(a); + + Vertices alphabets = getAlphabets(dict); + + cout << "Alphabets: "; + copy(alphabets.begin(), alphabets.end(), ostream_iterator(cout, " ")); + cout << endl; + + Graph g(alphabets); + createGraph(g, dict); + + g.printAdjList(); + topologicalSort(g); +} \ No newline at end of file