Fastrail08

Alien Dictionary (IMPORTANT)

Aug 18th, 2025
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.77 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. //creates directed edge from ch1 -> ch2 (where ch1 ∈ word1 , ch2 ∈ word2, first unmatched characters between word1& word2  respectively)
  5. // word1[i] = ch1, word2[i] = ch2, idx(word1) < idx(word2), word1 appears before word2 in dictionary
  6.  
  7. //ONLY the first mismatched letter provides a meaningful interpretation when these words are LEXICOGRAPHICALLY SORTED.
  8. //If char matches, move to the next index in both. First difference between the char is responsible for why they got sorted in this particular lexicographical order. When ch1 != ch2, word1 appears before word2 because ch1 is lexicographically smaller than ch2.
  9. // IMPORTANT is to break after the first mismatch, becuse the characters after that don't matter, because dictionary arranges letter on the basis of mismatch, first mismatch is the reason why they are listed as they are in a dictionary. That is what lexicographical order is
  10. void addEdge(string word1, string word2, vector<vector<int> > &adj){
  11.     for(int i = 0; i < word1.size() && i < word2.size(); i++){
  12.         if(word1[i] == word2[i]){
  13.             continue;
  14.         }
  15.         else{
  16.             int u = word1[i] - 'a', v = word2[i] - 'a';
  17.             adj[u][v] = 1;
  18.             break;
  19.         }
  20.     }
  21. }
  22.  
  23.  
  24. string bfs(vector<int> &indegree, vector<vector<int> > &adj){
  25.     queue<int> q;
  26.     string order = "";
  27.     for(int i = 0; i < indegree.size(); i++){
  28.         if(indegree[i] == 0){
  29.             q.push(i);
  30.         }
  31.     }
  32.    
  33.     while(!q.empty()){
  34.         int front = q.front();
  35.         q.pop();
  36.        
  37.         order += (char) (front + 'a');
  38.        
  39.         for(int i = 0; i < 26; i++){
  40.             if(adj[front][i] == 1){
  41.                 indegree[i]--;
  42.                 if(indegree[i] == 0){
  43.                     q.push(i);
  44.                 }
  45.             }
  46.         }
  47.     }
  48.    
  49.     return order;
  50. }
  51.  
  52. int main() {
  53.     // your code goes here
  54.     int n;
  55.     cin >> n;
  56.     vector<string> words(n);
  57.     for(int i = 0; i < n; i++){
  58.         cin >> words[i];
  59.     }
  60.     vector<vector<int> > adj(26, vector<int>(26, 0));
  61.     bool prefixContradiction = false;
  62.    
  63.     //create directed graph (lexicographical order of alphabets formed because of ordering of words)
  64.     for(int i = 0; i < words.size(); i++){
  65.         for(int j = i + 1; j < words.size(); j++){
  66.             //IMPORTANT PREFIX CHECK (ONLY thing that I missed while coding this problem)
  67.             //If words[j] is the prefix of words[i], then it violates the rule of dictionary
  68.             //words[i] = "abc" , words[j] = "ab" , as i < j
  69.             // In any dictionary, PREFIX of ANY WORD will APPEAR BEFORE it in a dictionary
  70.             // Ordering
  71.             if(words[i].size() > words[j].size() && words[i].substr(0, words[j].size()) == words[j]){
  72.                 prefixContradiction = true;
  73.                 cout << "NOT POSSIBLE TO FIND VALID ORDERING BECAUSE DICTIONARY CONTRADICTS ON PREFIX CONTRADICTION";
  74.                 break;
  75.             }
  76.             addEdge(words[i], words[j], adj);
  77.         }
  78.         if(prefixContradiction){
  79.                 break;
  80.         }
  81.     }
  82.    
  83.     if(!prefixContradiction){
  84.         vector<int> indegree(26, -1);
  85.    
  86.         // find out the total unique characters present in the dictionary
  87.         // Total number of nodes
  88.         int charCount = 0;
  89.         for(int i = 0; i < words.size(); i++){
  90.             for(int j = 0; j < words[i].size(); j++){
  91.                 int ch = words[i][j] - 'a';
  92.                 if(indegree[ch] == -1){
  93.                     indegree[ch] = 0;  
  94.                     charCount++;
  95.                 }
  96.             }
  97.         }
  98.        
  99.         // update indegree
  100.         for(int i = 0; i < 26; i++){
  101.             for(int j = 0; j < 26; j++){
  102.                 if(adj[i][j] == 1){
  103.                     indegree[j]++;
  104.                 }
  105.             }
  106.         }
  107.        
  108.         string ans = bfs(indegree, adj);
  109.        
  110.         //ONLY print answer if all the nodes can be topologically arranged
  111.         // TOPOLOGICALLY here means LEXICOGRAPHICALLY ARRANGED (no contradiction/cycles)
  112.         // words = [ab cd ef ad]
  113.         // (ab, cd) suggests 'a' should come before 'c' : a -> c
  114.         // (cd, ad) suggest 'c' should come before 'a': c -> a
  115.         // In dictionary it causes contradiction, but visually in graph it represents a cycle
  116.         // A valid ordering is ONLY printed, when ALL the characters can be topologically arranged
  117.         // which means NO nodes can be a member of a cycle OR NO cycle should be present in the graph.
  118.         if(ans.size() == charCount){
  119.             cout << ans << '\n';
  120.         }
  121.         else{
  122.             cout << "NOT POSSIBLE TO FIND VALID ORDERING BECAUSE DICTIONARY CONTRADICTS ON CYCLE CONTRADICTION";
  123.         }
  124.     }
  125.  
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment