1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| #include <cstdio> #include <cstring> #include <algorithm>
char pre[35], mid[35], val[1005]; int len, cnt = 0;
void find(int pre_left, int pre_right, int mid_left, int mid_right, int order) { if (mid_left > mid_right) return; val[order] = pre[pre_left], cnt = std::max(cnt, order); int root = mid_left; while (root <= mid_right && mid[root] != pre[pre_left]) root++; find(pre_left + 1, pre_left + root - mid_left, mid_left, root - 1, order << 1); find(pre_left + root - mid_left + 1, pre_right, root + 1, mid_right, order << 1 | 1); }
int main() { scanf("%s%s", pre, mid), len = strlen(pre); find(0, len - 1, 0, len - 1, 1); for (int i = 1; i <= cnt; i++) if (val[i]) printf("%c ", val[i]); else printf("NULL "); return 0; }
|