FZU 2218 Simple String Problem (状压DP)

来源:互联网 发布:mac移除windows分区 编辑:程序博客网 时间:2024/06/04 21:44

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




 Simple String Problem

Accept: 192    Submit: 415
Time Limit: 2000 mSec    Memory Limit : 32768 KB

 Problem Description

Recently, you have found your interest in string theory. Here is an interesting question about strings.

You are given a string S of length n consisting of the first k lowercase letters.

You are required to find two non-empty substrings (note that substrings must be consecutive) of S, such that the two substrings don't share any same letter. Here comes the question, what is the maximum product of the two substring lengths?

 Input

The first line contains an integer T, meaning the number of the cases. 1 <= T <= 50.

For each test case, the first line consists of two integers n and k. (1 <= n <= 2000, 1 <= k <= 16).

The second line is a string of length n, consisting only the first k lowercase letters in the alphabet. For example, when k = 3, it consists of a, b, and c.

 Output

For each test case, output the answer of the question.

 Sample Input

425 5abcdeabcdeabcdeabcdeabcde25 5aaaaabbbbbcccccdddddeeeee25 5adcbadcbedbadedcbacbcadbc3 2aaa

 Sample Output

6150210

 Hint

One possible option for the two chosen substrings for the first sample is "abc" and "de".

The two chosen substrings for the third sample are "ded" and "cbacbca".

In the fourth sample, we can't choose such two non-empty substrings, so the answer is 0.

 Source

第六届福建省大学生程序设计竞赛-重现赛(感谢承办方华侨大学)

Submit  Back  Status  Discuss



题意:一个长度为n的字符串,包含前k个字母,求所有任意两个子串包含不同字母个数乘积的最大值

解析:状压DP,第一次直接暴力状压DP,超时了,然后看大牛博客,优化之后过了,先n^2枚举所有子串包含的状态,然后再枚举所有状态,用<=当前状态去更新当前状态,最后遍历一遍,找含这种状态和不含这种状态乘积的最大值,例如当前状态cur,反面状态就是((1<<k)-1)^cur,就是简单位运算异或一下


借鉴大牛博客: http://blog.csdn.net/loy_184548/article/details/51158801

代码1(超时-----比较好理解所有状态):

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<map>#include<cmath>#include<string>#define N 6009using namespace std;const int INF = 0x3f3f3f3f;typedef long long LL;char s[N];int vis[17], b[(1<<16)+11];int main(){    int t, n, k;    scanf("%d", &t);    while(t--)    {        scanf("%d%d", &n, &k);        int m = 1<<k;        scanf(" %s", s);        for(int i = 0; i < m; i++)        {            memset(vis, 0, sizeof(vis));            for(int j = 0; j < k; j++) if((i>>j)&1) vis[j] = 1;            int sum = 0, cur = 0;            for(int j = 0; j < n; j++)            {                if(vis[s[j]-'a']) cur++;                else                {                    sum = max(sum, cur);                    cur = 0;                }            }            b[i] = max(sum, cur);        }        int ans = 0;        for(int i = 0; i < m; i++)            ans = max(ans, b[i]*b[i^(m-1)]);        cout << ans << endl;    }    return 0;}

代码2(AC):

#include<iostream>#include<algorithm>#include<cstdio>#include<cstring>#include<vector>#include<queue>#include<map>#include<cmath>#include<string>#define N 6009using namespace std;const int INF = 0x3f3f3f3f;typedef long long LL;char s[N];int dp[(1<<16)+11];int main(){    int t, n, k;    scanf("%d", &t);    while(t--)    {        scanf("%d%d", &n, &k);        int m = 1<<k;        scanf(" %s", s);        memset(dp, 0, sizeof(dp));        for(int i = 0; i < n; i++)        {            int state = 0;            for(int j = i; j < n; j++)            {                state = state|(1<<(s[j] - 'a'));                dp[state] = max(dp[state], j - i + 1);            }        }        for(int i = 0; i < m; i++)        {            for(int j = 0; j < k; j++)            {                if(i&(1<<j))                {                    int state = i - (1<<j);                    dp[i] = max(dp[i], dp[state]);                }            }        }        int ans = 0;        for(int i = 0; i < m; i++)            ans = max(ans, dp[i]*dp[i^(m-1)]);        cout << ans << endl;    }    return 0;}



0 0
原创粉丝点击