hdu 1502 Regular Words

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1502

Problem Description
Consider words of length 3n over alphabet {A, B, C} . Denote the number of occurences of A in a word a as A(a) , analogously let the number of occurences of B be denoted as B(a), and the number of occurenced of C as C(a) .

Let us call the word w regular if the following conditions are satisfied:

A(w)=B(w)=C(w) ;
if c is a prefix of w , then A(c)>= B(c) >= C(c) .
For example, if n = 2 there are 5 regular words: AABBCC , AABCBC , ABABCC , ABACBC and ABCABC .

Regular words in some sense generalize regular brackets sequences (if we consider two-letter alphabet and put similar conditions on regular words, they represent regular brackets sequences).

Given n , find the number of regular words.
 

Input
There are mutiple cases in the input file.

Each case contains n (0 <= n <= 60 ).

There is an empty line after each case.
 

Output
Output the number of regular words of length 3n .

There should be am empty line after each case.
 

Sample Input
  
  
2 3
 

Sample Output
  
  
5 42
 

Source


题意是求长度为3*n的串中前缀中A的个数大于B的个数大于C的个数。。用dp[i][j][k]表示前缀串符合的个数,则有状态转移方程:

dp[i][j][k]=dp[i-1][j][k]+dp[i][j-1][k]+dp[i][j][k-1]

java代码:

//package eg1;

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




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值