给定外语的排序字典(单词数组),找到该语言中字符的顺序。
例子:
Input: words[] = {"baa", "abcd", "abca", "cab", "cad"}
Output: Order of characters is 'b', 'd', 'a', 'c'
Note that words are sorted and in the given language "baa"
comes before "abcd", therefore 'b' is before 'a' in output.
Similarly we can find other orders.
Input: words[] = {"caa", "aaa", "aab"}
Output: Order of characters is 'c', 'a', 'b'
这个想法是创建一个字符图,然后找到所创建图的拓扑排序。以下是详细步骤。
1)创建一个图g ,其顶点数等于给定外语中字母的大小。例如,如果字母大小为5,则单词中可以有5个字符。最初图中没有边。
2)对给定排序数组中的每对相邻单词进行跟随。
…..a)令当前的单词对为word1和word2 。逐个比较两个单词的字符并找到第一个不匹配的字符。
…..b)在g中从word1的不匹配字符到word2的不匹配边缘。
3)打印上面创建的图的拓扑排序。
以下是上述算法的实现。
C++
// A C++ program to order of characters in an alien language
#include
using namespace std;
// Class to represent a graph
class Graph
{
int V; // No. of vertices'
// Pointer to an array containing adjacency listsList
list *adj;
// A function used by topologicalSort
void topologicalSortUtil(int v, bool visited[], stack &Stack);
public:
Graph(int V); // Constructor
// function to add an edge to graph
void addEdge(int v, int w);
// prints a Topological Sort of the complete graph
void topologicalSort();
};
Graph::Graph(int V)
{
this->V = V;
adj = new list[V];
}
void Graph::addEdge(int v, int w)
{
adj[v].push_back(w); // Add w to v’s list.
}
// A recursive function used by topologicalSort
void Graph::topologicalSortUtil(int v, bool visited[], stack &Stack)
{
// Mark the current node as visited.
visited[v] = true;
// Recur for all the vertices adjacent to this vertex
list::iterator i;
for (i = adj[v].begin(); i != adj[v].end(); ++i)
if (!visited[*i])
topologicalSortUtil(*i, visited, Stack);
// Push current vertex to stack which stores result
Stack.push(v);
}
// The function to do Topological Sort. It uses recursive topologicalSortUtil()
void Graph::topologicalSort()
{
stack Stack;
// Mark all the vertices as not visited
bool *visited = new bool[V];
for (int i = 0; i < V; i++)
visited[i] = false;
// Call the recursive helper function to store Topological Sort
// starting from all vertices one by one
for (int i = 0; i < V; i++)
if (visited[i] == false)
topologicalSortUtil(i, visited, Stack);
// Print contents of stack
while (Stack.empty() == false)
{
cout << (char) ('a' + Stack.top()) << " ";
Stack.pop();
}
}
int min(int x, int y)
{
return (x < y)? x : y;
}
// This function finds and prints order of characer from a sorted
// array of words. n is size of words[]. alpha is set of possible
// alphabets.
// For simplicity, this function is written in a way that only
// first 'alpha' characters can be there in words array. For
// example if alpha is 7, then words[] should have only 'a', 'b',
// 'c' 'd', 'e', 'f', 'g'
void printOrder(string words[], int n, int alpha)
{
// Create a graph with 'aplha' edges
Graph g(alpha);
// Process all adjacent pairs of words and create a graph
for (int i = 0; i < n-1; i++)
{
// Take the current two words and find the first mismatching
// character
string word1 = words[i], word2 = words[i+1];
for (int j = 0; j < min(word1.length(), word2.length()); j++)
{
// If we find a mismatching character, then add an edge
// from character of word1 to that of word2
if (word1[j] != word2[j])
{
g.addEdge(word1[j]-'a', word2[j]-'a');
break;
}
}
}
// Print topological sort of the above created graph
g.topologicalSort();
}
// Driver program to test above functions
int main()
{
string words[] = {"caa", "aaa", "aab"};
printOrder(words, 3, 3);
return 0;
}
Java
// A Java program to order of
// characters in an alien language
import java.util.*;
// Class to represent a graph
class Graph
{
// An array representing the graph as an adjacency list
private final LinkedList[] adjacencyList;
Graph(int nVertices)
{
adjacencyList = new LinkedList[nVertices];
for (int vertexIndex = 0; vertexIndex < nVertices; vertexIndex++)
{
adjacencyList[vertexIndex] = new LinkedList<>();
}
}
// function to add an edge to graph
void addEdge(int startVertex, int endVertex)
{
adjacencyList[startVertex].add(endVertex);
}
private int getNoOfVertices()
{
return adjacencyList.length;
}
// A recursive function used by topologicalSort
private void topologicalSortUtil(int currentVertex, boolean[] visited,
Stack stack)
{
// Mark the current node as visited.
visited[currentVertex] = true;
// Recur for all the vertices adjacent to this vertex
for (int adjacentVertex : adjacencyList[currentVertex])
{
if (!visited[adjacentVertex])
{
topologicalSortUtil(adjacentVertex, visited, stack);
}
}
// Push current vertex to stack which stores result
stack.push(currentVertex);
}
// prints a Topological Sort of the complete graph
void topologicalSort()
{
Stack stack = new Stack<>();
// Mark all the vertices as not visited
boolean[] visited = new boolean[getNoOfVertices()];
for (int i = 0; i < getNoOfVertices(); i++)
{
visited[i] = false;
}
// Call the recursive helper function to store Topological
// Sort starting from all vertices one by one
for (int i = 0; i < getNoOfVertices(); i++)
{
if (!visited[i])
{
topologicalSortUtil(i, visited, stack);
}
}
// Print contents of stack
while (!stack.isEmpty())
{
System.out.print((char)('a' + stack.pop()) + " ");
}
}
}
public class OrderOfCharacters
{
// This function finds and prints order
// of characer from a sorted array of words.
// alpha is number of possible alphabets
// starting from 'a'. For simplicity, this
// function is written in a way that only
// first 'alpha' characters can be there
// in words array. For example if alpha
// is 7, then words[] should contain words
// having only 'a', 'b','c' 'd', 'e', 'f', 'g'
private static void printOrder(String[] words, int alpha)
{
// Create a graph with 'aplha' edges
Graph graph = new Graph(alpha);
for (int i = 0; i < words.length - 1; i++)
{
// Take the current two words and find the first mismatching
// character
String word1 = words[i];
String word2 = words[i+1];
for (int j = 0; j < Math.min(word1.length(), word2.length()); j++)
{
// If we find a mismatching character, then add an edge
// from character of word1 to that of word2
if (word1.charAt(j) != word2.charAt(j))
{
graph.addEdge(word1.charAt(j) - 'a', word2.charAt(j)- 'a');
break;
}
}
}
// Print topological sort of the above created graph
graph.topologicalSort();
}
// Driver program to test above functions
public static void main(String[] args)
{
String[] words = {"caa", "aaa", "aab"};
printOrder(words, 3);
}
}
//Contributed by Harikrishnan Rajan
输出:
c a b
时间复杂度:创建图形的第一步需要O(n + alhpa)时间,其中n是给定单词的数量,而alpha是给定字母的字符数量。第二步也是拓扑排序。请注意,图中将有alpha顶点和最多(n-1)个边。拓扑排序的时间复杂度是O(V + E),在这里是O(n + aplha)。因此,总体时间复杂度为O(n + aplha)+ O(n + aplha),即O(n + aplha)。