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
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