hdu 1502 Regular Word

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

 

题目描述:

 

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

 

 

 

题目大意:

第一行给你一个整数n,代表abc三个字母每个有n个,现在问你有多少种排列方法,使得前任意项都有a的数目>=b的数目>=c的数目

 

题目分析;

状态转移方程:dp[x][y][z]=dp[x-1][y][z]+dp[x][y-1][z]+dp[x][y][z-1]

 

Ac代码:

 

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
using namespace std;
char num[65][105],dp[65][65][65][105];

void sum(char *a,char *b)
{
    int l1=a[0],l2=b[0],lmax;//a[0],b[0]代表字符串长度
    a[0]=lmax=max(l1,l2);
    for(int i=1;i<=lmax;i++)//高精度
    {
        a[i]+=b[i];
        if(a[i]>=10)
        {
            a[i+1]++;
            a[i]%=10;
            if(i+1>lmax)
            {
                a[0]++;
                lmax++;
            }
        }
    }
}

void cpy(char *a,char *b)
{
    for(int i=0;i<=b[0];i++)
        a[i]=b[i];
}

void LCS(int x,int y,int z)
{   
    //状态转移方程
    if(x>=1&&x-1>=y&&y>=z)
        sum(dp[x][y][z],dp[x-1][y][z]);
    if(y>=1&&x>=y-1&&y-1>=z)
        sum(dp[x][y][z],dp[x][y-1][z]);
    if(z>=1&&x>=y&&y>=z-1)
        sum(dp[x][y][z],dp[x][y][z-1]);
}

void Init()
{
    memset(dp,0,sizeof(dp));
    dp[0][0][0][0]=1;
    dp[0][0][0][1]=1;
    for(int i=1;i<=60;i++)
    {
        for(int j=0;j<=i;j++)
        {
            for(int k=0;k<=j;k++)
            {
                LCS(i,j,k);
                if(i==j&&j==k)
                    cpy(num[i],dp[i][j][k]);
            }
        }
    }
}

int main()
{
    Init();
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=num[n][0];i>=1;i--)
            printf("%d",num[n][i]);
        printf("\n\n");
    }
    return 0;
}

 

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值