1 ij 2 abc 3 def 4 gh 5 kl 6 mn 7 prs 8 tuv 9 wxy 0 oqz |
No solution.
”. If there are more solutions having the minimum number of words, you can choose any single one of them.
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 7 const int MAXV = 110; 8 const int MAXS = 50010; 9 const int MAXL = 55; 10 const int MAXE = MAXS * MAXL; 11 12 int number[26] = {2, 2, 2, 3, 3, 3, 4, 4, 1, 1, 5, 5, 6, 6, 0, 7, 0, 7, 7, 8, 8, 8, 9, 9, 9, 0}; 13 int mat[MAXV][MAXV], pre[MAXV], ecnt, n, k; 14 int s[MAXS][MAXL], src[MAXV], len[MAXS]; 15 char ans[MAXS][MAXL], str[MAXV]; 16 17 void init() { 18 memset(mat, -1, sizeof(mat)); 19 } 20 21 void add_edge(int u, int v, int pos) { 22 mat[u][v] = pos; 23 } 24 25 int dis[MAXV]; 26 27 bool SPFA() { 28 memset(dis, 0x3f, sizeof(dis)); 29 dis[0] = 0; 30 for(int i = 0; i < n; ++i) { 31 for(int j = i + 1; j <= n; ++j) { 32 if(~mat[i][j] && dis[i] + 1 < dis[j]) { 33 pre[j] = mat[i][j]; 34 dis[j] = dis[i] + 1; 35 } 36 } 37 } 38 return dis[n] <= n; 39 } 40 41 void getFail(int P[], int m, int f[]) { 42 f[0] = f[1] = 0; 43 for(int i = 1; i < m; ++i) { 44 int j = f[i]; 45 while(j && P[i] != P[j]) j = f[j]; 46 f[i + 1] = (P[i] == P[j] ? j + 1 : 0); 47 } 48 } 49 50 void KMP(int T[], int n, int P[], int m, int f[], int pos) { 51 getFail(P, m, f); 52 for(int i = 0, j = 0; i < n; ++i) { 53 while(j && P[j] != T[i]) j = f[j]; 54 if(P[j] == T[i]) ++j; 55 if(j == m) add_edge(i - m + 1, i + 1, pos); 56 } 57 } 58 59 void print(int pos) { 60 if(pos - len[pre[pos]] != 0) { 61 print(pos - len[pre[pos]]); 62 putchar(' '); 63 } 64 printf("%s", ans[pre[pos]]); 65 } 66 67 int fail[MAXL]; 68 69 int main() { 70 while(scanf("%s", str) != EOF) { 71 if(strcmp(str, "-1") == 0) break; 72 n = strlen(str); 73 for(int i = 0; i < n; ++i) src[i] = str[i] - '0'; 74 scanf("%d", &k); 75 for(int i = 0; i < k; ++i) { 76 scanf("%s", ans[i]); 77 len[i] = strlen(ans[i]); 78 for(int j = 0; j < len[i]; ++j) s[i][j] = number[ans[i][j] - 'a']; 79 } 80 init(); 81 for(int i = 0; i < k; ++i) 82 KMP(src, n, s[i], len[i], fail, i); 83 if(SPFA()) print(n), puts(""); 84 else puts("No solution."); 85 } 86 }