【題目描述】樹的凹入表示法主要用於樹的屏幕或打印輸出,其表示的基本思想是兄弟間等長,一個結點的長度要不小於其子結點的長度。二叉樹也可以這樣表示,假設葉結點的長度爲1,一個非葉結點的長度等於它的左右子樹的長度之和。 一棵二叉樹的一個結點用一個字母表示(無重複),輸出時從根結點開始: 每行輸出若干個結點字符(相同字符的個數等於該結點長度), 如果該結點有左子樹就遞歸輸出左子樹; 如果該結點有右子樹就遞歸輸出右子樹。 假定一棵二叉樹一個結點用一個字符描述,現在給出先序和中序遍歷的字符串,用樹的凹入表示法輸出該二叉樹。 【輸入】兩行,每行是由字母組成的字符串(一行的每個字符都是唯一的),分別表示二叉樹的先序遍歷和中序遍歷的序列。 【輸出】行數等於該樹的結點數,每行的字母相同。 【輸入樣例】ABCDEFG CBDAFEG 【輸出樣例】AAAA BB C D EE F G |
#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <cmath>
#include <map>
#include <set>
#include <queue>
#include <stack>
#define sf(a) scanf("%d\n",&a)
#define pf(a) printf("%.2lf\n",a)
#define pfc(a) printf("%c",a)
#define pi acos(-1.0)
#define E 1e-8
#define ms(a) memset(a,0,sizeof a)
#define min_ms(a) memset(a,-1,sizeof a)
#define max_ms(a) memset(a,0x3f,sizeof a)
#define rep(a,b,c) for(int a=b;a<=c;a++)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef struct node;
typedef node *tree;
const int inf=0x3f3f3f3f;
const int idata=2e6+5;
const int mod=1e9+7;
struct node
{
char data;
node *lchild;
node *rchild;
};
string s1,s2;
int cnt[idata];
int calc(int l1,int r1,int l2,int r2)
{
if(l1==r1) return cnt[l1]=1;
int i;
for(i=l2;i<=r2;i++)
{
if(s1[l1]==s2[i]) break;
}
int p=i-l2;
if(i>l2) cnt[l1]+=calc(l1+1,p+l1,l2,i);
if(i<r2) cnt[l1]+=calc(l1+p+1,r1,i+1,r2);
return cnt[l1];
}
signed main()
{
/*cin.tie(0);
ios::sync_with_stdio(false);*/
while(cin>>s1>>s2)
{
calc(0,s1.length()-1,0,s2.length()-1);
for(int i=0;i<s1.length();i++)
{
for(int j=1;j<=cnt[i];j++)
{
cout<<s1[i];
}
cout<<endl;
}
exit(0);
}
return 0;
}