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. 模拟赛 Problem 1 高级打字机(type.cpp/c/pas)

    Problem 1 高级打字机(type.cpp/c/pas) [题目描述] 早苗入手了最新的高级打字机.最新款自然有着与以往不同的功能,那就是它具备撤销功能,厉害吧. 请为这种高级打字机设计一个程序 ...

  2. 多Tabs的横向滚动插件(支持Zepto和jQuery)

    一. 效果图 二. 功能介绍 1. 支持横向移动 2. 支持点击Tab后该Tab居中 3. 拉到最左边和最右边后依然可以拉动,只是tabs的移动距离变小. 三. 使用说明 1. 在你的html中添加T ...

  3. 不折移动web不腾--开启我的个人Mac之旅

    背景,非常久非常久曾经(听过)Linux,瞎玩 Mac mini,而今Linux下开发技能半身不遂,处于放任状态.明明就知道随着时间流逝会越陌生的东西越不想去抓住最后的余温,不知道这算不算放弃,反正迟 ...

  4. Android Message和obtainMessage的差别

    前几天须要实现一个以太网功能就看了以太网的源代码部分,看见了源代码部分在消息处理时,发现有一些不同的地方:   平时我在处理消息时:   1.首先创建Handler对象:   private Hand ...

  5. 推断给定的IP地址是否是内网IP

    /** * 推断给定的IP地址是否是内网IP * * @author GaoHuanJie */ public class Test{ public boolean isInnerIP(String ...

  6. 推断client手机类型,并跳转到对应的app下载页面

    实现的原理,是检測浏览器的 USER-AGENT 这个header,然后依据正則表達式来确定client类型. 假设都不匹配,Fallback回退策略是显示相应的页面.让用户自己选择. 适合採用二维码 ...

  7. 启动第二个Activity

    启动第二个Activity activity_main.xml文件: <? xml version="1.0" encoding="utf-8"?> ...

  8. 改动grub默认启动顺序

    grub如今有两个版本号,一个grub,一个grub2,两个版本号的操作不太一样. 装centos的朋友非常有可能是grub.我电脑装的是ubuntu14.04,为grub2.我演示grub2的过程. ...

  9. C# Json反序列化 C# 实现表单的自动化测试<通过程序控制一个网页> 验证码处理类:UnCodebase.cs + BauDuAi 读取验证码的值(并非好的解决方案) 大话设计模式:原型模式 C# 深浅复制 MemberwiseClone

    C# Json反序列化   Json反序列化有两种方式[本人],一种是生成实体的,方便处理大量数据,复杂度稍高,一种是用匿名类写,方便读取数据,较为简单. 使用了Newtonsoft.Json,可以自 ...

  10. Java原型模式之浅拷贝-深拷贝

    一.是什么? 浅拷贝:对值类型的成员变量进行值的复制,对引用类型的成员变量仅仅复制引用,不复制引用的对象 深拷贝:对值类型的成员变量进行值的复制,对引用类型的成员变量也进行引用对象的复制 内部机制: ...