Sicily 1210 二叉树

来源:互联网 发布:文明5汉化补丁mac 编辑:程序博客网 时间:2024/05/19 13:45

关于树的前序和后序遍历,当前序遍历的第二个元素出现在后序遍历的倒数第二位,以后序遍历倒数第三位起向左数都是子树的元素,但是左右不确定,此时有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;}    

原创粉丝点击