Regular Words
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 2089 Accepted Submission(s): 822
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.
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.
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.
There should be am empty line after each case.
Sample Input
2 3
Sample Output
5 42
题意:输入n,求一个包含n个A,n个B,n个C的字符串,其任意的前缀都满足A的个数大于B的个数,B的个数大于C的个数,这个字符串有多少种可能。
思路:dp[i][j][k]分别表示i个A,j个B,k个C组成满足条件的字符串有多少种可能,因为这个字符串最后一位要么是1,要么是2,要 么是3,所以就有状态转移方程:dp[i][j][k] = dp[i-1][j][k]+dp[i][j-1][k]+dp[i][j][k-1]。
由于到n=60时种类数太大了,所以用int会超内存,所以我都换成了char。
输入个数太多。。。所以只能先打一下表,然后再直接输出。
#include<bits/stdc++.h>
using namespace std;
char dp[61][61][61][101];
void add(int i, int j, int k){
for(int l = 1; l <= 100; l++){
if(i-1 >= j && j >= k &&i-1>=0&&dp[i-1][j][k][l]!=0)dp[i][j][k][l] += dp[i-1][j][k][l];
if(i >= j-1 && j-1 >= k &&j-1>=0&&dp[i][j-1][k][l]!=0)dp[i][j][k][l] += dp[i][j-1][k][l];
if(i >= j && j >= k-1 &&k-1>=0&&dp[i][j][k-1][l]!=0)dp[i][j][k][l] += dp[i][j][k-1][l];
}
for(int l = 1; l <= 100; l++){
int temp = dp[i][j][k][l]/10;
dp[i][j][k][l+1]+=temp;
dp[i][j][k][l]%=10;
}
}
int main(){
int n;
dp[0][0][0][1] = 1;
for(int i = 0; i <= 60; i++)
for(int j = 0; j <= 60; j++)
for(int k = 0; k <= 60; k++)
if(i >= j && j >= k && i+j+k!=0)
add(i,j,k);
while(scanf("%d",&n)!=EOF){
int flag = 0;
for(int i = 100; i >= 1; i--){
if(dp[n][n][n][i]!=0)flag = 1;
if(flag)printf("%c",dp[n][n][n][i]+'0');
}
printf("\n\n");
}
}