Description
Problem
Expression bracketing
Input:Standard Input
Output:Standard output
Time limit:1 second
Memory limit:32 MB
Inthis problem you will have to find in how many waysNLetters can be bracketed so that the bracketing is non-binarybracketing. For example4Lettershave11Possible bracketing:
XXXX, (XX) xx, x (XX) x, XX (XX), (XXX) x, x (XXX), (XX) x) X, (x (XX) x, (xx), x (XX) X), x (XX )). Of these the first sixbracketing are not binary. Given the number of letters you will have to findthe Total Number of non-binary bracketing.
Input
Theinput file contains several lines of input. Each line contains a single integerN (0 <n <= 26). Input isterminated by end of file.
Output
For each line of input produce one line of outputwhich denotes the number of Non binary bracketingNLetters.
Sample Input
3
4
5
10
Sample output
1
6
31
98187
If p and q are the required strings, then (p, q) also satisfies and finds all impossible conditions.
Train of Thought: We first want to satisfy, as you can imagine, this is similar to the concept of catlan number, all of which divide strings into (1, n-1), (2, n-2 ).... but all the situations may be hard to find.
Super catalan numberBut note that the number of catlands starts from 0.
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>typedef long long ll;using namespace std;const int maxn = 30;int n;ll catalan[maxn], supper[maxn];void init() {supper[0] = supper[1] = supper[2] = 1;for (int i = 3; i < maxn; i++) supper[i] = (3*(2*i-3)*supper[i-1] - (i-3)*supper[i-2])/i;catalan[0] = catalan[1] = 1;catalan[2] = 2;catalan[3] = 5;for (int i = 4; i < maxn; i++) for (int j = 0; j < i; j++)catalan[i] += catalan[j] * catalan[i-j-1];}int main() {init();while (scanf("%d", &n) != EOF) {printf("%lld\n", supper[n]-catalan[n-1]);}return 0;}