您的位置:首页 > 其它

Sicily 1210 二叉树

2012-01-26 21:10 447 查看
关于树的前序和后序遍历,当前序遍历的第二个元素出现在后序遍历的倒数第二位,以后序遍历倒数第三位起向左数都是子树的元素,但是左右不确定,此时有2种情况,计数器对应*2。最后吐槽一下坑爹的输出格式。

#include <stdio.h>
#include <string.h>
int ctr = 1;
char s1[30];
char s2[30];
void solve( int x1, int y1, int x2, int y2 ) {
int po,i;
if ( x1>=y1 )
return;
for ( i=x2;i<=y2;i++ ) {
if ( s1[x1+1]==s2[i] ) {
po=i;
break;
}
}
if ( po==y2-1 )
ctr=ctr*2;
solve( x1+1, x1+1+po-x2, x2, po );
solve( x1+2+po-x2, y1, po+1, y2-1 );
}
int main()
{

int l1,l2;

while (scanf( "%s",&s1 )!=EOF ) {
scanf( "%s",&s2 );
l1=strlen(s1);
l2=strlen(s2);
ctr=1;
solve(0,l1-1,0,l2-1);
printf( "%d",ctr );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: