FZU-2218 Simple String Problem(状态压缩DP)
生活随笔
收集整理的這篇文章主要介紹了
FZU-2218 Simple String Problem(状态压缩DP)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
原題地址: 題意: 給你一個串和兩個整數n和k,n表示串的長度,k表示串只有前k個小寫字母,問你兩個不含相同元素的連續子串的長度的最大乘積。 思路: 狀態壓縮DP最多16位,第i位的狀態表示第i位字母是否存在, 代碼: #include<iostream>
#include<algorithm>
#include<cstdio>
#include<queue>
#include<map>
#include<vector>
#include<cstring>
#include<cmath>
#define eps 1e-12
using namespace std;
typedef long long ll;
const ll mo = 1000000007, N = 2*1e3+10;
char s[N];
int dp[(1<<16)+100];
int main()
{ int t; cin>>t; while(t--) { int n, m; scanf("%d%d", &n, &m); scanf("%s", s); memset(dp, 0, sizeof(dp)); for(int i = 0; i<n; i++) { int t = 0; for(int j = i; j<n; j++) { t |= 1<<(s[j] - 'a'); dp[t] = max(dp[t], j - i + 1); } } int s = 1<<m; for(int i = 0; i<s; i++) { for(int j = 0; j<m; j++) { if((1<<j) & i) dp[i] = max(dp[i], dp[i^(1<<j)]); } } int ans = 0; for(int i = 0; i<s; i++) { ans = max(ans, dp[i]*dp[(s-1)^i]); } cout<<ans<<endl; } return 0;
}
轉載于:https://www.cnblogs.com/luowentao/p/8997048.html
總結
以上是生活随笔為你收集整理的FZU-2218 Simple String Problem(状态压缩DP)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: .net EF监控 MiniProfil
- 下一篇: 20155201 网络攻防技术 实验六