FZU 2218 Simple String Problem (状压DP)

本文介绍了一道关于字符串的问题,使用状态压缩动态规划的方法来解决。通过优化算法避免超时,分享了两种不同的实现思路及其对应的源代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题目链接: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

4
25 5
abcdeabcdeabcdeabcdeabcde
25 5
aaaaabbbbbcccccdddddeeeee
25 5
adcbadcbedbadedcbacbcadbc
3 2
aaa

 Sample Output

6
150
21
0

 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

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




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

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


借鉴大牛博客: http://blog.youkuaiyun.com/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 6009
using 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 6009
using 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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值