HDU1502 Regular Words

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1502

思路:当只有两个数时,可以用卡特兰数做,当三个数时,没想到卡特兰数的做法。可以使用动态规划。

状态转移方程如下:

dp[i][j][k] = dp[i - 1][j][k] + dp[i][j - 1][k] + dp[i][j][k - 1]

代码如下:

import java.math.BigInteger;
import java.util.Scanner;
public class Main{

    public static void main(String[] args){
        Scanner cin =new Scanner(System.in);
        BigInteger dp[][][] = new BigInteger[61][61][61];
        dp[0][0][0] = BigInteger.ZERO;
        for(int i = 0; i < 2;  i++){
            for(int j = 0; j <= i; j++){
                for(int k = 0; k <= j; k++){
                    dp[i][j][k] = BigInteger.ONE;
        }
        }
        }

        int n;
        while(cin.hasNext()){
            n = cin.nextInt();
            for(int i = 2; i <= n ; i++){
                for(int j = 0; j <=i; j++){
                    for(int k = 0; k <= j; k++){
                        dp[i][j][k] = BigInteger.ZERO;
                        if(i - 1 >= j){
                            dp[i][j][k] = dp[i][j][k].add(dp[i -  1][j][k]);
                        }
                        if(j - 1 >= k){
                            dp[i][j][k] = dp[i][j][k].add(dp[i][j - 1][k]);
                        }
                        if(k - 1 >= 0){
                            dp[i][j][k] = dp[i][j][k].add(dp[i][j][k - 1]);
                        }
                    }
                }
            }
            System.out.println(dp[n][n][n]);
            System.out.println();
        }
    }
}
时间: 2024-11-12 16:50:04

HDU1502 Regular Words的相关文章

计数 组合数学动态规划总结

本文持续更新 对计数,组合数学DP作总结,给出思路,状态转移方程,略去代码,状态初始值等. 1 划分数 m个不可区分的物品分成n份,每份的数量大于等于0,求划分的方法数. 思路: 若m < n, 则等价于m个物品划分为m份. 否则,若至少存在1份数量为0,则相当于m个物品划分为n - 1份;若每份数量大于等于1,则相当于m - n个物品划分为n份.动态规划或记忆化搜索. 2 HDU1502 Regular Words 给定n,求n个A,n个B,n个C组成的串的任意前缀中A的数量大于等于B的数量,

LeetCode 10. Regular Expression Matching

https://leetcode.com/problems/regular-expression-matching/description/ Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover

Python正则表达式Regular Expression基本用法

资料来源:http://blog.csdn.net/whycadi/article/details/2011046   直接从网上资料转载过来,作为自己的参考.这个写的很清楚.先拿来看看. 1.正则表达式re模块的基本函数. (1)findall函数的用法 findall(rule,target[,flag])是在目标字符串中找到符合规则的字符串.参数说明:rule表示规则,target表示目标字符串,[,flag]表示的是规则选项.返回的结果是一个列表.若没找到符合的,是一个空列表. 如: 因

[转]8 Regular Expressions You Should Know

Regular expressions are a language of their own. When you learn a new programming language, they're this little sub-language that makes no sense at first glance. Many times you have to read another tutorial, article, or book just to understand the "s

codeforce 550 D Regular Bridge

题意:建立一个连通图,它的所有点的度为k,且至少含有一个桥. 做法:先建立一个桥,再在桥两边建立两个度为k的连通图,通过这个桥连接在一起. 很显然k为偶数的时候无解. #include<map> #include<string> #include<cstring> #include<cstdio> #include<cstdlib> #include<cmath> #include<queue> #include<v

Java [leetcode 10] Regular Expression Matching

问题描述: Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. http://i.cnblogs.com/EditPosts.aspx?opt=1 The matching should cover the entire input string

【leetcode】Regular Expression Matching (hard) ★

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

[LeetCode] Regular Expression Matching(递归)

Implement regular expression matching with support for '.' and '*'. '.' Matches any single character. '*' Matches zero or more of the preceding element. The matching should cover the entire input string (not partial). The function prototype should be

Python正则表达式 re(regular expression)

1. 点. .: 代表一个字符 (这个跟linux的正则表达式是不同的,那里.代表的是后面字符的一次或0次出现) 2. 转义 \\ 或者 r'\': 如 r'python\.org' (对.符号的转义) 3. ^ 非或叫做排除 如[^abc]: 任何以非a,b,c的字符 4. | 选择符 如python|perl (从python和perl选择一个) 也可以: p(ython|erl) 5. ? 可选项 如: r'(http://)?(www\.)?python\.org' (http://和w