Problem statement
You are given N subsequences(not necessarily contiguous) of a string. Find the shortest possible string which has distinct lower case letters, with the given subsequences. The solution is guaranteed to exist.
Input
The first line has the value of N, followed by N lines containing each of the N subsequences of the solution string.
Each subsequence has distinct lower case letters
1<=N<=100000
Output
Output the value of the solution string.
Example
Input:
2
acd
bc
Output:
abcd
second test case
Input:
3
ac
dbe
dea
Output:
dbeac
void toposort(unordered_map<char,unordered_set<char>>& map, unordered_map<char, bool>& visited, stack<char>& st, char c) { visited[c] = true; for(auto u:map[c]) { if(!visited.count(u)) toposort(map, visited, st, u); } st.push(c); } string findOrder(vector<string>& strs) { unordered_map<char,unordered_set<char>> map; unordered_map<char, bool> visited; unordered_set<char> set; stack<char> st; for(auto& s:strs) { for(unsigned i=1; i<s.size(); i++) { map[s[i-1]].insert(s[i]); set.insert(s[i]); } set.insert(s[0]); } for(auto k:set) { if(!visited.count(k)) { toposort(map, visited, st, k); } } string res; while(!st.empty()) { res += st.top(); st.pop(); } return res; }