Poj 1240 pre-post-erous! Solution report

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.