Regular Words

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2102    Accepted Submission(s): 825

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
 
还是看题解做的,蓝瘦。。。
dp[i][j][k][100]; i这维表示a的个数,j这维表示b的个数,k这维表示c的个数。
dp过程中需要用大数加法。
 
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std; void add(char A[],char B[],char C[])
{
int a=;
int len1=strlen(A);
int len2=strlen(B);
int wei=;
while(a<len1&&a<len2)
{
int sum=A[a]+B[a]-''-''+wei;
wei=;
if(sum>)
{
wei++;
sum-=;
}
C[a]=sum+'';
a++;
}
while(a<len1)
{
int sum=A[a]+wei-'';
wei=;
if(sum>)
{
wei++;
sum-=;
}
C[a]=sum+'';
a++;
}
while(a<len2)
{
int sum=B[a]+wei-'';
wei=;
if(sum>)
{
wei++;
sum-=;
}
C[a]=sum+'';
a++;
}
if(wei>)
C[a++]='';
C[a]='\0';
} char dp[][][][]; int main()
{
char A[],B[],C[];
for(int i=; i<=; i++)
for(int j=; j<=; j++)
for(int k=; k<=; k++)
strcpy(dp[i][j][k],"");
strcpy(dp[][][],""); for(int i=; i<=; i++)
for(int j=; j<=; j++)
for(int k=; k<=; k++)
{
if(i>=j&&j>=k)
{
add(dp[i-][j][k],dp[i][j-][k],dp[i][j][k]);
add(dp[i][j][k],dp[i][j][k-],dp[i][j][k]);
}
}
int n;
while(scanf("%d",&n)!=EOF)
{
int len=strlen(dp[n][n][n]);
char res[];
for(int i=len-;i>=;i--)
res[len-i-]=dp[n][n][n][i];
res[len]='\0';
printf("%s\n\n",res);
//getchar();
}
return ;
}
 

HDU_1502_dp的更多相关文章

随机推荐

  1. BZOJ(1) 1003 [ZJOI2006]物流运输

    1003: [ZJOI2006]物流运输 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 9404  Solved: 4087[Submit][Stat ...

  2. 深入解析Microsoft Sql server 2008

    http://blog.csdn.net/downmoon/article/details/5256548

  3. 《WF in 24 Hours》读书笔记 - Hour 1 - Understanding Windows Workflow Foundation

    1.1 Hour 1 - Understanding Windows Workflow Foundation   1.1.1 What workflow is in general A workflo ...

  4. vijos- P1383盗窃-黑珍珠 (python + 代码优化)

    P1383盗窃-黑珍珠 Accepted 标签:怪盗基德 VS OIBH[显示标签] 背景 怪盗基德 VS OIBH 第二话 描写叙述 今次怪盗基德再次对阵OIBH,目标是Black Star!基德已 ...

  5. [Angular] New in V6.1

    Router Scroll Position Restoration: remember and restore scroll position as the user navigates aroun ...

  6. HDOJ题目4417 Super Mario(划分树求区间比k小的个数+二分)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Tota ...

  7. sgu101Domino

    给你一些边,假设存在欧拉路径就打出来 我的代码例如以下: #include<iostream> #include<cstring> using namespace std; i ...

  8. Android 录制屏幕的实现方法

    Android 录制屏幕的实现方法 Chrome   2017-02-15 15:32:01 发布 您的评价:       5.0   收藏     0收藏 长久以来,我一直希望能够直接从Androi ...

  9. 跟我一起写Makefile:概述

    什么是makefile?也许非常多Winodws的程序猿都不知道这个东西.由于那些Windows的集成开发环境(integrateddevelopment environment,IDE)都为你做了这 ...

  10. 利用jQuery Ajax技术实现每隔5秒向某页面传值

    有时候我们须要每隔一段时间向某页面传值,比方说聊天室,每隔几秒就像数据库处理页面传值并取回,然后显示在聊天窗体.又或者是每隔一段时间就查询用户最后发言时间到如今是否间隔2分钟.假设是则将用户退出. 这 ...