标签:bin ack list input sign col logs order color
Description
D
/
/
B E
/ \
/ \
A C G
/
/
F
Input
Output
Sample Input
DBACEGF ABCDEFG BCAD CBAD
Sample Output
ACBFGED CDAB
Source
#include<iostream> #include<cstdio> #include<cmath> #include<cstring> #include<sstream> #include<algorithm> #include<queue> #include<deque> #include<iomanip> #include<vector> #include<cmath> #include<map> #include<stack> #include<set> #include<functional> #include<fstream> #include<memory> #include<list> #include<string> using namespace std; typedef long long LL; typedef unsigned long long ULL; #define N 31 #define INF 1000000009 #define eps 0.00000001 #define sf(a) scanf("%d",&a) const int MAXN = 1e5 + 3; char pre[N], mid[N]; int L[MAXN], R[MAXN]; int build(int l1, int r1, int l2, int r2)//l1 r1 是在前序序列的范围 l2 r2 是中序的范围 { if (l1 > r1 || l2 > r2) return -1; if (l1 == r1&&l2 == r2) return l1; char tr = pre[l1]; int p = l2; for (; p <= r2; p++) { if (mid[p] == tr) break; } L[l1] = build(l1 + 1, l1 + p - l2, l2, p - 1); R[l1] = build(l1 + p - l2 + 1, r1, p + 1, r2); return l1; } void print(int p) { if (L[p] != -1) print(L[p]); if (R[p] != -1) print(R[p]); printf("%c", pre[p]); } int main() { while (scanf("%s%s", pre, mid) != EOF) { memset(L, -1, sizeof(L)); memset(R, -1, sizeof(R)); int l = strlen(pre); int root = build(0, l - 1, 0, l - 1); print(root); printf("\n"); } }
标签:bin ack list input sign col logs order color
原文地址:http://www.cnblogs.com/joeylee97/p/7469760.html