#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<iostream>
using namespace std;
char a1[100],a2[100];
struct node
{
int data;
struct node *lchild,*rchild;
};
struct node *creat(int n,char *a1,char *a2)
{
struct node*root;
int i;
if(n==0)
return NULL;
root = (struct node*)malloc(sizeof(struct node));
root->data=a2[n-1];
for(i=0;i<n;i++)
{
if(a1[i]==a2[n-1])
break;
}
root->lchild=creat(i,a1,a2);
root->rchild=creat(n-i-1,a1+1+i,a2+i);
return root;
};
int depth(struct node *root)
{
int ld,rd,depthval;
if(!root)
depthval = 0;
else
{
ld=depth(root->lchild);
rd=depth(root->rchild);
depthval=1+(ld>rd?ld:rd);
}
return depthval;
}
int main()
{
int t,len;
ios::sync_with_stdio(false);cin.tie(0);
struct node *root;
while (cin>>t)
{
while(t--)
{
cin>>a1;
cin>>a2;
len=strlen(a1);
root=creat(len,a1,a2);
cout<<depth(root)<<endl;
}
}
return 0;
}
2804 求二叉树的深度
最新推荐文章于 2022-01-22 23:18:33 发布