Problem A : King Is a Dull Boy
Description
King is a dull boy and not good at math. One day he met a simple problem but couldn’t find the answer, so he asked you for help. Here is the question.
Given n pairs of parentheses (‘(’ and ‘)’) and m pairs of square brackets (‘[’ and ‘]’), can you find the number of all well-formed expressions of brackets?
For example, if n = 1 and m = 1, there are totally 4 different well-formed expressions of brackets,
Note that expressions like “([]”, "[)", "([)]" or “[(])” are not well-formed.
Input
There are several test cases, each of which consists of two non-negative integers n and m, where 0 ≤ n + m ≤ 8.
Output
For each test case, output the number of all well-formed expressions of brackets.
Sample Input
1 1 2 0
Sample Output
4 2
Author: Sherry
题意:输入一个n和一个m,表示n个()和m个[]组成的串,问有多少种是合法的?
不会卡特兰数,数据不大 就直接暴力打表了
用一个栈判断是否合法,把结果都打出来
#include <bits/stdc++.h>
using namespace std;
int mp[10][10];
int main()
{
mp[0][0]=0;
mp[0][1]=1;
mp[0][2]=2;
mp[0][3]=5;
mp[0][4]=14;
mp[0][5]=42;
mp[0][6]=132;
mp[0][7]=429;
mp[0][8]=1430;
mp[1][0]=1;
mp[1][1]=4;
mp[1][2]=15;
mp[1][3]=56;
mp[1][4]=210;
mp[1][5]=792;
mp[1][6]=3003;
mp[1][7]=11440;
mp[2][0]=2;
mp[2][1]=15;
mp[2][2]=84;
mp[2][3]=420;
mp[2][4]=1980;
mp[2][5]=9009;
mp[2][6]=40040;
mp[3][0]=5;
mp[3][1]=56;
mp[3][2]=420;
mp[3][3]=2640;
mp[3][4]=15015;
mp[3][5]=80080;
mp[4][0]=14;
mp[4][1]=210;
mp[4][2]=1980;
mp[4][3]=15015;
mp[4][4]=100100;
mp[5][0]=42;
mp[5][1]=792;
mp[5][2]=9009;
mp[5][3]=80080;
mp[6][0]=132;
mp[6][1]=3003;
mp[6][2]=40040;
mp[7][0]=429;
mp[7][1]=11440;
mp[8][0]=1430;
int n,m;
while(cin>>n>>m)
{
cout<<mp[n][m]<<endl;
}
return 0;
}