#include #include #include #include using std::map; using std::vector; using std::string; typedef map edges; typedef vector trie; trie build_trie(vector & patterns) { trie t; size_t node; size_t n = 1; t.push_back(edges()); for (size_t i = 0; i < patterns.size(); i++) { node = 0; const string& pattern = patterns[i]; for (size_t j = 0; j < pattern.size(); j++) { char c = pattern[j]; int next = t[node][c]; if (next != 0) { node = next; } else { t[node][c] = n; node = n; t.push_back(edges()); n += 1; } } } return t; } int main() { size_t n; std::cin >> n; vector patterns; for (size_t i = 0; i < n; i++) { string s; std::cin >> s; patterns.push_back(s); } trie t = build_trie(patterns); for (size_t i = 0; i < t.size(); ++i) { for (const auto & j : t[i]) { std::cout << i << "->" << j.second << ":" << j.first << "\n"; } } return 0; }