HDU1502 Regular Words(DP+大数模拟)

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


题意:问满足条件(A.B.C各有N个,且任意前缀满足A的个数大于等于B的个数,B的个数大于等于C的个数)的字符串的个数。每组数据后单独输出一行空行。


思路:令dp[i][j][k]表示字符串中有i个A,j个B,k个C。且满足i>j>k。在dp[i-1][j][k],dp[i][j-1][k],dp[i][j][k-1]构成的字符串后面加上一个字符一定能构成dp[i][j][k]。

所以dp[i][j][k]=dp[i-1][j][k]+dp[i][j-1][k]+dp[i][j][k-1];

需要注意的是,N<=60,结果太大,连longlong都不能存下,需要用字符串存,并模拟大数加法即可。


#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <string>
using namespace std;

string dp[65][65][65];

string sum(string s1,string s2)
{
    if(s1.length()<s2.length())
    {
        string temp=s1;
        s1=s2;
        s2=temp;
    }
    int i,j;
    for(i=s1.length()-1,j=s2.length()-1;i>=0;i--,j--)
    {
        s1[i]=char(s1[i]+(j>=0?s2[j]-'0':0));   //注意细节
        if(s1[i]-'0'>=10)
        {
            s1[i]=char((s1[i]-'0')%10+'0');
            if(i) s1[i-1]++;
            else s1='1'+s1;
        }
    }
    return s1;
}

int main()
{
    for(int i=0;i<=60;i++)
    {
        for(int j=0;j<=60;j++)
        {
            for(int k=0;k<=60;k++)
            {
                dp[i][j][k]="0";
            }
        }
    }
    dp[0][0][0]="1";
    for(int i=0;i<=60;i++)
    {
        for(int j=0;j<=i;j++)
        {
            for(int k=0;k<=j;k++)
            {
                if(k!=0) dp[i][j][k]=sum(dp[i][j][k],dp[i][j][k-1]);
                if(j!=0) dp[i][j][k]=sum(dp[i][j][k],dp[i][j-1][k]);
                if(i!=0) dp[i][j][k]=sum(dp[i][j][k],dp[i-1][j][k]);
            }
        }
    }
    int n;
    while(cin>>n)
    {
        cout<<dp[n][n][n]<<endl;
        cout<<endl;
    }
    return 0;
}

推荐一个大数模板的总结博文:http://blog.csdn.net/y990041769/article/details/20116995

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值