Simple String Problem FZU - 2218 (状压dp)

本文探讨了一道关于字符串的问题,旨在寻找两个不包含相同字符的子串,并最大化它们长度的乘积。通过状态压缩动态规划的方法,文章详细介绍了如何高效解决这一问题,包括状态转移方程的设计和实现。

Simple String Problem

FZU - 2218

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.

因为最多有16中字母,所以可以用dp状态压缩,用int中的前16位每为表示一个字母是否出现。先求出字母状态为i的最大长度dp[i]为多少,再求出dp[i]表示的字母状态i,及其子集的最大长度,更新dp[i],为什么求它的子集呢,因为首先子集状态的串可能比当前状态串要长,而且我们要求的两个子串没有相同的字母也就是两串01二进制串按位与为0,而其中一个串的子集状态只会在原有的1上变成0,所以它的子集状态与另一个串按位与仍然为0。所以要保存下它和它子集状态最长字符串,最后再用dp[i] * dp[s^i]求出最大值,s是全1状态和任何串异或后相当于全部取反正式我们想要的按位与为0的两个串。

code:

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int dp[(1<<16)+10];
char s[2005];
int main(){
    int t;
    scanf("%d",&t);
    while(t--){
        int n,k;
        memset(dp,0,sizeof(dp));
        scanf("%d%d",&n,&k);
        scanf("%s",s);
        for(int i = 0; i < n; i++){
            int m = 0;
            for(int j = i; j < n; j++){//枚举子串记录每种状态的最长长度
                m |= 1 << (s[j] - 'a');//更新状态
                dp[m] = max(dp[m],j-i+1);
            }
        }
        int s = (1 << k) - 1;
        for(int i = 1; i <= s; i++){
            for(int j = 0; j < k; j++){
                if(i & (1 << j))//如果i状态中存在j位置的字符
                    dp[i] = max(dp[i],dp[i^(1<<j)]);//异或除去这个字符看i状态和其子集的最长长度
            }
        }
        int ans = 0;
        for(int i = 1; i <= s; i++){
            int m = s ^ i;
            ans = max(ans,dp[i] * dp[m]);
        }
        printf("%d\n",ans);
    }
    return 0;
}

## 软件功能详细介绍 1. **文本片段管理**:可以添加、编辑、删除常用文本片段,方便快速调用 2. **分组管理**:支持创建多个分组,不同类型的文本片段可以分类存储 3. **热键绑定**:为每个文本片段绑定自定义热键,实现一键粘贴 4. **窗口置顶**:支持窗口置顶功能,方便在其他应用程序上直接使用 5. **自动隐藏**:可以设置自动隐藏,减少桌面占用空间 6. **数据持久化**:所有配置和文本片段会自动保存,下次启动时自动加载 ## 软件使用技巧说明 1. **快速添加文本**:在文本输入框中输入内容后,点击"添加内容"按钮即可快速添加 2. **批量管理**:可以同时编辑多个文本片段,提高管理效率 3. **热键冲突处理**:如果设置的热键与系统或其他软件冲突,会自动提示 4. **分组切换**:使用分组按钮可以快速切换不同类别的文本片段 5. **文本格式化**:支持在文本片段中使用换行符和制表符等格式 ## 软件操作方法指南 1. **启动软件**:双击"大飞哥软件自习室——快捷粘贴工具.exe"文件即可启动 2. **添加文本片段**: - 在主界面的文本输入框中输入要保存的内容 - 点击"添加内容"按钮 - 在弹出的对话框中设置热键和分组 - 点击"确定"保存 3. **使用热键粘贴**: - 确保软件处于运行- 在需要粘贴的位置按下设置的热键 - 文本片段会自动粘贴到当前位置 4. **编辑文本片段**: - 选中要编辑的文本片段 - 点击"编辑"按钮 - 修改内容或热键设置 - 点击"确定"保存修改 5. **删除文本片段**: - 选中要删除的文本片段 - 点击"删除"按钮 - 在确认对话框中点击"确定"即可删除
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值