树的遍历。输入前序和中序输出后序。前中后就是根的位置,话句话说,输入顺序为根左右,左根右,输出左右根。递归即可。
用指针虽然会慢一点,但好处就是不必担心个数,然而,每次结束之后要记得释放内存,不然遇到数据量较大的Trie,不释放是会爆内存的,多写几行总比返回MLE要强吧。
#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<string>
#include<queue>
#include<cmath>
#include<set>
///LOOP
#define REP(i, n) for(int i = 0; i < n; i++)
#define FF(i, a, b) for(int i = a; i < b; i++)
#define FFF(i, a, b) for(int i = a; i <= b; i++)
#define FD(i, a, b) for(int i = a - 1; i >= b; i--)
#define FDD(i, a, b) for(int i = a; i >= b; i--)
///INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RFI(n) scanf("%lf", &n)
#define RFII(n, m) scanf("%lf%lf", &n, &m)
#define RFIII(n, m, k) scanf("%lf%lf%lf", &n, &m, &k)
#define RFIV(n, m, k, p) scanf("%lf%lf%lf%lf", &n, &m, &k, &p)
#define RS(s) scanf("%s", s)
///OUTPUT
#define PN printf("\n")
#define PI(n) printf("%d\n", n)
#define PIS(n) printf("%d ", n)
#define PS(s) printf("%s\n", s)
#define PSS(s) printf("%s ", n)
///OTHER
#define PB(x) push_back(x)
#define CLR(a, b) memset(a, b, sizeof(a))
#define CPY(a, b) memcpy(a, b, sizeof(b))
#define display(A, n, m) {REP(i, n){REP(j, m)PIS(A[i][j]);PN;}}
using namespace std;
typedef long long LL;
typedef pair<int, int> P;
const int MOD = 100000000;
const int INFI = 1e9 * 2;
const LL LINFI = 1e17;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int N = 1111;
const int M = 33;
const int move[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, 1, -1, -1, 1, -1, -1};
struct tree
{
char ch;
tree *lson, *rson;
}root;
char s1[M], s2[M];
void build(tree *p, int a, int b, int c)
{
p -> ch = s1[a];
p -> lson = p -> rson = NULL;
if(c - b == 1)return;
FF(i, b, c)if(s2[i] == s1[a])
{
if(i > b)
{
tree *t = new tree();
p -> lson = t;
build(t, a + 1, b, i);
}
if(i < c - 1)
{
tree *t = new tree();
p -> rson = t;
build(t, a + i - b + 1, i + 1, c);
}
return;
}
}
void print(tree *p)
{
if(p -> lson != NULL)print(p -> lson);
if(p -> rson != NULL)print(p -> rson);
putchar(p -> ch);
}
void del(tree *p)
{
if(p -> lson != NULL)del(p -> lson);
if(p -> rson != NULL)del(p -> rson);
if(p != &root)delete p;
}
int main()
{
//freopen("input.txt", "r", stdin);
//freopen("output.txt", "w", stdout);
int l;
while(cin >> s1 >> s2)
{
l = strlen(s2);
build(&root, 0, 0, l);
print(&root);
del(&root);
PN;
}
return 0;
}