#include #include using namespace std; list::iterator najdi(list &a, string s) { list::iterator i; for (i = a.begin(); i != a.end(); ++i) { if (*i == s) break; } return i; } void posouvej(string &s2, string &s, list &b, list &a) { while (s2 != s) { printf("%s ", s.c_str()); b.pop_front(); s = b.front(); } printf("%s", s2.c_str()); a.pop_front(); b.pop_front(); if (s != ".") printf(" "); } void merge2(list &a, list &b) { while (b.size() > 0) { string s = b.front(); string s2 = a.front(); list::iterator i = najdi(a, s); list::iterator j = najdi(b, s2); if (i == a.end() && j == b.end()) { if (s < s2) { printf("%s ", s.c_str()); b.pop_front(); } else { printf("%s ", s2.c_str()); a.pop_front(); } } else if (i == a.end() && j != b.end()) { posouvej(s2, s, b, a); } else if (i != a.end() && j == b.end()) { posouvej(s, s2, a, b); } else { if (s < s2) { posouvej(s2, s, b, a); } else { posouvej(s, s2, a, b); } } } printf("\n"); } bool delsi(list &a, list &b) { if (a.size() < b.size()) return true; if (b.size() > a.size()) return false; for (list::iterator i = a.begin(), j = b.begin(); i != a.end(); ++i, ++j) { if ((*i) < (*j)) return true; if ((*i) > (*j)) return false; } return true; } void merge(list &a, list &b) { if (delsi(b, a)) merge2(b, a); else merge2(a, b); } void read(list &a) { a.clear(); char s[100]; scanf("%s", s); while (strcmp(s, ".") != 0) { a.push_back(s); scanf("%s", s); } a.push_back(s); } int main() { list a, b; read(a); while (a.front() != ".") { read(b); merge(a, b); read(a); } return 0; }