Question:
Returns the number of trees in descending order before the M-tree.
Solution:
We know that the first point of the forward traversal must be the root node, and the last point of the backward traversal must be the root node.
Therefore, we only need to determine how many son nodes each node has, and then multiply by C (M, K ).
Code
#include <iostream>#include <algorithm>#include <string>using namespace std;string sq, sh;int len, ans,m;int Combination (int n, int m){int ans = 1;for (int i = 1; i <= m; i++)ans = ans * (n - i + 1) / i;return ans;}void make (int l, int r, int t, int w) {if (l > r || t > w) return;int p = 0, i = 0;while (l <= r) {char s = sq[l];for (i = 0; i < len; i++) if (sh[i] == s) break;int k = w - i+1;make (l+1, l+k-1, i+1, w);l = l + k;w=i-1;p++;}ans*=Combination(m,p);}int main() {while (cin >>m>> sq >> sh) {len = (int) sq.size() - 1;ans=1;reverse (sh.begin(), sh.end() );make (1, len, 1, len);cout<<ans<<endl;}return 0;}
Poj 1240 pre-post-erous! Solution report