不知道为什么分到DP分类里面去了,(pos1,pos2)表示一颗子数的前序遍历 (pos3,pos4)表示该颗子数的后序遍历
然后可以将该子树分成更小的子树,直到pos1==pos2
每次的种类数就是 子树的种类树相乘*C( M,子树个数)
AC代码如下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
using namespace std;
char s1[100], s2[100];
int M;
int Cn( int n, int k ){
int ans = 1;
for( int i = 1; i <= k; i++ ){
ans *= n;
n--;
}
for( int i = 1; i <= k; i++ ){
ans /= i;
}
return ans;
}
int DFS( int pos1, int pos2, int pos3, int pos4 ){
if( pos1 == pos2 ){
return 1;
}
int a, b, c, d;
a = b = pos1 + 1;
c = d = pos3;
int ans = 1, temp = 0;
while( a <= pos2 ){
while( s2[d] != s1[a] ) d++;
b = a + d - c;
ans *= DFS( a, b, c, d );
temp++;
a = b = b + 1;
c = d = d + 1;
}
return ans * Cn( M, temp );
}
int main(){
while( scanf( "%d", &M ) && M ){
scanf( "%s%s", s1, s2 );
cout << DFS( 0, strlen( s1 ) - 1, 0, strlen( s2 ) - 1 ) << endl;
}
return 0;
}