#include #include #include char in[256]; int l, n; struct wd { struct wd *next; int len; char x[1]; }; #define HASH 8192 struct wd *ht[HASH]; static unsigned char *cvt = "@22233344115566070778889990@@@@@"; static void *mjalloc(int size) { static char *heap, *this; static int rest; if (rest < size) { rest = 16384; heap = malloc(rest); if (!heap) { fprintf(stderr, "Out of memory!\n"); exit(99); } } this = heap; heap += size; rest -= size; return this; } static unsigned int hash(unsigned char *b, unsigned int l) { unsigned int i = l; while (l--) i = i*17 + cvt[*b++ & 0x1f]; return i & (HASH-1); } static unsigned int hash2(unsigned char *b, unsigned int l) { unsigned int i = l; while (l--) i = i*17 + *b++; return i & (HASH-1); } static int gs(FILE *f, char *b) { char *c; fgets(b, 255, f); c = strchr(b, '\r'); if (c) *c = 0; c = strchr(b, '\n'); if (c) *c = 0; return c - b; } static void rd(void) { FILE *f; int i; char buf[256]; f = fopen("phone.in", "r"); if (!f) exit(1); l = gs(f, in); fscanf(f, "%d\n", &n); for(i=0; ilen = l; h = hash(buf, l); w->next = ht[h]; ht[h] = w; strcpy(w->x, buf); } fclose(f); } int count[256]; struct wd *prev[256]; #define INF 10000 static struct wd *find(char *pos, int l) { int h = hash2(pos, l); struct wd *w = ht[h]; while (w) { if (w->len == l) { char *x, *y; x = pos; y = w->x; while (*y && *x == cvt[*y & 0x1f]) x++, y++; if (!*y) return w; } w = w->next; } return NULL; } static void solve(void) { int i, j; count[0] = 0; for(i=1; i<=l; i++) count[i] = INF; for(i=0; i count[i] + 1) { count[j] = count[i] + 1; prev[j] = w; } } } static void wrr(FILE *f, int l) { struct wd *w = prev[l]; l -= w->len; if (l) { wrr(f, l); fputc(' ', f); } fputs(w->x, f); } static void wr(void) { FILE *g; g = fopen("phone.out", "w"); if (!g) exit(3); if (count[l] < INF) { wrr(g, l); fputc('\n', g); } else fprintf(g, "No solution.\n"); fclose(g); } int main(void) { rd(); solve(); wr(); return 0; }