14 typedef std::pair<const std::string, Node> WordNode;
16 WordNode *down, *right, *up;
21 # error "`TREE' not defined or bungled constant setting"
24 typedef std::map<std::string, Node> Map;
25 #elif TREE == CXX_UOMAP
26 # include <unordered_map>
27 typedef std::unordered_map<std::string, Node> Map;
29 # error "unknown `TREE' value"
34 static void bail(const char *msg, ...)
39 va_start(ap, msg); std::vfprintf(stderr, msg, ap); va_end(ap);
40 pos = std::ftell(stdin);
41 if (pos >= 0) std::fprintf(stderr, " (input pos = %ld)", pos);
42 std::fputc('\n', stderr);
46 static void print_chain(WordNode *wnode)
48 const std::string *word = &wnode->first;
49 Node *node = &wnode->second;
52 std::fputs(word->c_str(), stdout);
54 { std::putchar(' '); print_chain(node->down); }
56 std::fputs("{ ", stdout);
58 std::fputs(word->c_str(), stdout);
60 { std::putchar(' '); print_chain(node->down); }
61 wnode = node->right; if (!wnode) break;
62 std::fputs(" | ", stdout);
63 word = &wnode->first; node = &wnode->second;
65 std::fputs(" }", stdout);
72 char buf[WORDMAX]; size_t n;
75 if (!std::fgets(buf, WORDMAX, stdin)) break;
76 n = std::strlen(buf); assert(n);
77 if (buf[n - 1] == '\n') buf[--n] = 0;
78 else if (n == WORDMAX - 1) bail("word too long");
81 if (ch = std::getchar(), ch != EOF)
82 bail("short read, found `%c'", ch);
85 auto r = map.insert(WordNode(std::string(buf, n), Node{}));
86 if (!r.second) printf(";; duplicate `%s'\n", buf);
88 Node *node = &r.first->second;
89 node->len = 1; node->up = node->down = node->right = 0;
95 for (auto p = map.begin(); p != map.end(); ++p) {
96 /* std::fprintf(stderr, ";; ponder `%.*s'\n",
97 p->first.length(), p->first.c_str()); */
98 WordNode *wnode = &*p, *pwnode;
99 const std::string *word = &wnode->first;
100 Node *node = &wnode->second;
101 if (word->length() <= 1)
104 /* std::fprintf(stderr, ";; search `%.*s'\n",
105 w->first.length(), w->first.c_str()); */
106 std::string prefix{*word, 0, word->length() - 1};
107 auto p = map.find(prefix); pwnode = p == map.end() ? 0 : &*p;
109 int nlen = node->len;
112 Node *parent = &pwnode->second;
113 nlen++; int plen = parent->len;
116 else if (plen == nlen) {
117 node->right = parent->down; parent->down = wnode;
120 parent->down = wnode; node->right = 0;
122 wnode = pwnode; node = &wnode->second; pwnode = node->up;
125 if (nlen > max) max = nlen;
128 for (auto p = map.begin(); p != map.end(); ++p)
129 if (p->second.len == max) { print_chain(&*p); std::putchar('\n'); }