第六届福建省大学生程序设计竞赛——G Simple String Problem(状态压缩dp)

来源:互联网 发布:淘宝买家留言怎么修改 编辑:程序博客网 时间:2024/04/30 13:26

题目链接:http://acm.fzu.edu.cn/problem.php?pid=2218

题目大意:

解题思路:

PS: 因为这道题目自己当时没有做出来,是下来一位巨巨给的题解,其实我的代码也和他的也就差不多了,几乎是一样的了。他真的有一万种方法AC,真心膜拜。所以,我只是为了来贴代码,记录下这道状态压缩dp。

Show me the code!

#include <cstdio>using namespace std;const int maxn = 2000;int dp[1 << 16];char str[maxn];int max(int a, int b) {    return a >= b ? a : b;}void fill(int *s, int *e, int val) {while (s != e) {*s = val; s++;}}int main() {    int T;    scanf("%d", &T);    while (T--) {        int N, M;        scanf("%d%d%s", &N, &M, str);        fill(dp, dp + (1 << M), 0);        for (int a = 0; a < N; ++a) {            for (int b = a, t = 0; b < N; ++b) {                t |= 1 << (str[b] - 'a');                dp[t] = max(dp[t], b - a + 1);            }        }        for (int a = 0; a < 1 << M; ++a) {            for (int b = 0; b < M; ++b) {                if (a >> b & 1) {                    dp[a] = max(dp[a], dp[a ^ (1 << b)]);                }            }        }        int res = 0;        for (int a = 0; a < 1 << M; ++a) {            res = max(res, dp[a] * dp[(1 << M) - 1 ^ a]);        }        printf("%d\n", res);    }    return 0;}
0 0
原创粉丝点击