|
| 1 | +#define PROBLEM "https://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1227" |
| 2 | + |
| 3 | +#include <iostream> |
| 4 | +#include <vector> |
| 5 | +#include <algorithm> |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +#define call_from_test |
| 9 | +#include "../../../marathon/trace.cpp" |
| 10 | +#undef call_from_test |
| 11 | + |
| 12 | +string char_to_digit = "22233344455566677778889999"; |
| 13 | + |
| 14 | +int solve_testcases() { |
| 15 | + int N; cin >> N; |
| 16 | + if (N == 0) return 1; |
| 17 | + vector<string> words(N); |
| 18 | + for(auto &e : words) cin >> e; |
| 19 | + string seq; cin >> seq; |
| 20 | + |
| 21 | + vector<string> word_digits(N); |
| 22 | + for(int i=0; i<N; i++) { |
| 23 | + string digits = ""; |
| 24 | + for(char c : words[i]) { |
| 25 | + digits += char_to_digit[c - 'a']; |
| 26 | + } |
| 27 | + word_digits[i] = digits; |
| 28 | + } |
| 29 | + |
| 30 | + Trace<string> trace; |
| 31 | + vector<int> terminals; |
| 32 | + auto go = [&](auto &&self, int pos, int id) -> void { |
| 33 | + if (pos == seq.size()) { |
| 34 | + terminals.emplace_back(id); |
| 35 | + return; |
| 36 | + } |
| 37 | + for(int i=0; i<N; i++) { |
| 38 | + if (pos + word_digits[i].size() > seq.size()) continue; |
| 39 | + if (seq.substr(pos, word_digits[i].size()) == word_digits[i]) { |
| 40 | + int nxt = pos + word_digits[i].size(); |
| 41 | + self(self, nxt, trace.add(words[i], id)); |
| 42 | + } |
| 43 | + } |
| 44 | + }; |
| 45 | + go(go, 0, -1); |
| 46 | + for(int id : terminals) { |
| 47 | + vector<string> words = trace.get(id); |
| 48 | + for(int i=0; i<(int)words.size(); i++) { |
| 49 | + cout << words[i]; |
| 50 | + if (i + 1 < (int)words.size()) cout << " "; |
| 51 | + else cout << ".\n"; |
| 52 | + } |
| 53 | + } |
| 54 | + cout << "--" << endl; |
| 55 | + return 0; |
| 56 | +} |
| 57 | + |
| 58 | +int main() { |
| 59 | + while(!solve_testcases()); |
| 60 | + return 0; |
| 61 | +} |
0 commit comments