#include<map> #include<iostream> #include<vector> #include<string> using namespace std; struct edge; struct node { char value; vector<struct edge *> edges; bool visited; int rank; node (char v): value(v), visited(false), rank(INT_MIN){} }; struct edge { node* s; node* v; edge(node * src, node* dst):s(src), v(dst){} }; map<char, node*> build_graph(vector<string>& input) { map<char, node*> graph; map<char, node*>::iterator found; for (int i = 0; i < input.size(); i++) { node * s = 0; node * v = 0; for (int j = 0; j<input[i].length(); j++) { if (!s) { if((found=graph.find(input[i][j]))!=graph.end()) { s = found->second; } else { s = new node(input[i][j]); graph[s->value] = s; } continue; } if((found=graph.find(input[i][j]))!=graph.end()) { v = found->second; } else { v = new node(input[i][j]); graph[v->value] = v; } s->edges.push_back(new edge(s, v)); s = v; } } return graph; } void print_sequence(node * cur) { if (!cur) return; cout<<cur->value<<", "; if (cur->edges.size()) print_sequence(cur->edges[0]->v); } void find_letter_sequence(vector<string>& input) { map<char, node*> graph = build_graph(input); print_sequence(graph[input[0][0]]); } int main() { vector<string>input; input.push_back("abcdefo"); input.push_back("begz"); input.push_back("hijk"); input.push_back("abcedefgh"); input.push_back("ikom"); find_letter_sequence(input); return 1; }