遍历问题[CODEVS1029]解题报告

思路:当这棵树有两个子树时,ans=dfs(l[root])*(r[root]);

    当这棵树有一个子树时,ans=dfs(child[root])<<1;

    当这棵树没有子树时,ans=1;

递归即可。

但是这里我写的时候一不小心写错了一个问题,就是第一种情况应该是乘而非加。。

#include
using namespace std;
#include
#include
#include
#include
typedef short hd;
hd qpos[27],hpos[27];
char qm[28],hb[28];
int dfs(hd ql,hd qr,hd hl,hd hr){
    if(ql==qr)
        return 1;
    if(qm[ql+1]==hb[hr-1])return dfs(ql+1,qr,hl,hr-1)<<1;
    return dfs(ql+1,hpos[hr-1]-1,hl,qpos[ql+1])*dfs(hpos[hr-1],qr,qpos[ql]+1,hr-1);
}
int main(){
    scanf("%s%s",qm,hb);
    hd ql=strlen(qm);
    for(hd i=0;i

小浩浩还提供了一种更简洁更优秀的思路:

其ans若是增加,必是因为其出现了仅有一棵子树的情况(<<1),所以我们只要统计只有一棵子树的节点个数n,输出2<

你可能感兴趣的:(组合数学,DFS)