Zoj 2711 regular words DP, "high-dimensional" catlan number (8-h)

Source: Internet
Author: User

Question address: http://acm.zju.edu.cn/onlinejudge/showProblem.do? Problemcode = 2711.

Solution: 1 records num [I] [J] [K], which indicates that the length of a is I + J + k starting from the first character. The number of A is I, the number of B is J, and the number of C is K.

If I> = j> = K, the last character is A, B, or C, which can be counted in three categories. Assume that the last character is, because the requirements of the question are prefix, so the number of the above method is exactly num [I-1] [J] [k]

In the other two cases, when adding a subscript, note that the subscript is less than zero.

2. At the beginning, all elements are assigned 0 values. In this way, in the triple for, the amount that does not satisfy the ijk inequality is not taken as the left value.

3. The reason for this calculation is that the data required for I, j, and K must have been calculated before ~

(Here, we only obtain a transpose for this three-dimensional matrix, without affecting the final result num [N] [N] [[N])

import java.math.*;import java.util.*;import java.io.*;public class Main {      public static void main(String[] args) throws Exception {             BigInteger [][][] num=new BigInteger [65][65][65];                 for(int i=0;i<65;i++)         for(int j=0;j<65;j++)         for(int k=0;k<65;k++)            num[i][j][k]=BigInteger.ZERO;                 num[0][0][0]=BigInteger.ONE;         for(int i=0;i<65;i++)         for(int j=i;j<65;j++)         for(int k=j;k<65;k++)                     {           if(i>0)num[i][j][k]= num[i][j][k].add(num[i-1][j][k]);           if(j>0)num[i][j][k]= num[i][j][k].add(num[i][j-1][k]);           if(k>0)num[i][j][k]= num[i][j][k].add(num[i][j][k-1]);         }                                 Scanner cin=new Scanner(System.in);             while(cin.hasNext())             {               int n=cin.nextInt();                 System.out.println(num[n][n][n]);                 System.out.println();             }          }         }

Considering the question of matching parentheses, the length of a string is 2n and there are N "(" and N ")", how many legal formulas (which can meet the matching conditions) are there? The answer is C (2n, n)-C (2n, n-1) = C (2n, N)/(n + 1) = H (n) is the catlan number, you can also follow the idea of this question (of course, with high precision, the next article will overflow in the end)

#include<iostream>using namespace std;typedef long long inta;int main(){  inta f[50][50];  for(int i=0;i<50;i++)    for(int j=0;j<50;j++)         f[i][j]=0;  f[0][0]=1;  for(int i=0;i<50;i++)    for(int j=0;j<=i;j++)      {        if(i>0) f[i][j]+=f[i-1][j];        if(j>0) f[i][j]+=f[i][j-1];      }   for(int i=0;i<50;i++)     cout<<f[i][i]<<endl;}

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.