FZU2218 Simple String Problem(状压DP)
首先,定义S,表示前k个字符出现的集合,用二进制来压缩。
接下来,推出dp1[S],表示集合为S的子串的最长长度。
然后根据dp1[S]再推出dp2[S],表示集合为S或S的子集的子串的最长长度。
最后答案就是max(dp2[S]*dp2[补(S)])
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int d[<<];
int main(){
int t,n,k;
char str[];
scanf("%d",&t);
while(t--){
scanf("%d%d%s",&n,&k,str);
memset(d,,sizeof(d));
for(int i=; i<n; ++i){
int s=;
for(int j=i; j<n; ++j){
s|=<<str[j]-'a';
d[s]=max(d[s],j-i+);
}
}
for(int i=; i<(<<k); ++i){
for(int j=; j<k ;++j){
if((i>>j)&) d[i]=max(d[i],d[i&(~(<<j))]);
}
}
int res=;
for(int i=; i<(<<k); ++i){
res=max(res,d[i]*d[(~i)&((<<k)-)]);
}
printf("%d\n",res);
}
return ;
}
FZU2218 Simple String Problem(状压DP)的更多相关文章
- FZU - 2218 Simple String Problem 状压dp
FZU - 2218Simple String Problem 题目大意:给一个长度为n含有k个不同字母的串,从中挑选出两个连续的子串,要求两个子串中含有不同的字符,问这样的两个子串长度乘积最大是多少 ...
- FZU-2218 Simple String Problem(状态压缩DP)
原题地址: 题意: 给你一个串和两个整数n和k,n表示串的长度,k表示串只有前k个小写字母,问你两个不含相同元素的连续子串的长度的最大乘积. 思路: 状态压缩DP最多16位,第i位的状态表示第i位 ...
- CF11D A Simple Task(状压DP)
\(solution:\) 思路大家应该都懂: 状压DP:\(f[i][j]\),其中 \(i\) 这一维是需要状压的,用来记录19个节点每一个是否已经走过(走过为 \(1\) ,没走为 \(0\) ...
- CF11D-A Simple Task【状压dp】
正题 题目链接:https://www.luogu.com.cn/problem/CF11D 题目大意 给出\(n\)个点\(m\)条边的一张简单无向图,求它的简单环的个数. \(1\leq n\le ...
- fzu2218 Simple String Problem
Accept: 2 Submit: 16 Time Limit: 2000 mSec Memory Limit : 32768 KB Problem Description Recent ...
- cf 11D A Simple Task(状压DP)
题意: N个点构成的无向图,M条边描述这个无向图. 问这个无向图中共有多少个环. (1 ≤ n ≤ 19, 0 ≤ m) 思路: 例子: 4 6 1 2 1 3 1 4 2 3 2 4 3 4 答案: ...
- FZU - 2218 Simple String Problem(状压dp)
Simple String Problem Recently, you have found your interest in string theory. Here is an interestin ...
- zoj3777 Problem Arrangement(状压dp,思路赞)
The 11th Zhejiang Provincial Collegiate Programming Contest is coming! As a problem setter, Edward i ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
随机推荐
- [Effective JavaScript 笔记]第31条:使用Object.getPrototypeOf函数而不要使用__proto__属性
ES5引入Object.getPrototypeOf函数作为获取对象原型的标准API,但由于之前的很多js引擎使用了一个特殊的__proto__属性来达到相同的目的.但有些浏览器并不支持这个__pro ...
- [POJ1050]To the Max
[POJ1050]To the Max 试题描述 Given a two-dimensional array of positive and negative integers, a sub-rect ...
- Floyd算法 及其运用
#include<stdio.h> ][]; ][]; void floyd(int n) { ;k<=n;k++) { ;i<=n;i++) { ;j<=n;j++) ...
- QEMU 使用的镜像文件:qcow2 与 raw
qcow2 的基本原理 qcow2 镜像格式是 QEMU 模拟器支持的一种磁盘镜像.它也是可以用一个文件的形式来表示一块固定大小的块设备磁盘.与普通的 raw 格式的镜像相比,有以下特性: 更小的空间 ...
- Django authentication 使用方法
转自 : https://docs.djangoproject.com/en/1.8/topics/auth/customizing/
- Android studio 添加依赖
以前添加依赖总是到github上下载源码,再添加源码到module的依赖当中,其实在studio中,应该使用maven库. 比如在github上看到了sliding-menu这个项目,就应该到mave ...
- ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/lib
今天在linux里安装mysql,运行时遇到这样的错误 ERROR 2002 (HY000): Can't connect to local MySQL server through socket ' ...
- 六间房PK同时观看两方视频(绕过VIP限制)+直播状态批量监测
可交换两个视频位置,记住最后播放记录,游客VIP限制也能观看视频等功能. 使用方法: 1.先运行 6.cn.live.exe 分别打开两个主播房间的网页(VIP限制也能获取视频的文件名) (房间已满提 ...
- CStringUtf8ToUnicode
CString CStringUtf8ToUnicode( CString Utf8 ) { int wLen = 0; CString strUnicode; LPSTR pBufChar = NU ...
- Java for LeetCode 190 Reverse Bits
Reverse bits of a given 32 bits unsigned integer. For example, given input 43261596 (represented in ...