fzu2188 状压dp
Time Limit:2000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
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
Sample Output
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.
状压dp,解释在代码中注释掉了,自己看吧
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
const int maxn=;
int dp[<<+];
char str[];
int main(){
int t;
scanf("%d",&t);
while(t--){
int n,k;
memset(dp,,sizeof(dp));
scanf("%d%d",&n,&k);
scanf("%s",str);
for(int i=;i<n;i++){//我们将16种字母划分为16位,直接dp[0~1<<16-1],对于每一位,如果值为0,代表那么字母不存在,如果为1
//代表那个字母存在,
int t=;
for(int j=i;j<n;j++){
t|=<<(str[j]-'a');
dp[t]=max(dp[t],j-i+);//求出某种状态的最长序列,,,
}
} int s=<<k;
for(int i=;i<s;i++){
for(int j=;j<k;j++){
if(i&(<<j))//这里从少的字符遍历到多的字符,迭代累加,
dp[i]=max(dp[i],dp[i^(<<j)]);//每次逐位进行比较,如果i字符串中包含地j个字符,那么更新dp值
}
}
int ans=;
for(int i=;i<s;i++)
ans=max(ans,dp[i]*(dp[(s-)^i]));//calculate
printf("%d\n",ans); }
return ;
}
fzu2188 状压dp的更多相关文章
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
		
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
 - nefu1109 游戏争霸赛(状压dp)
		
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
 - poj3311 TSP经典状压dp(Traveling Saleman Problem)
		
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
 - [NOIP2016]愤怒的小鸟 D2 T3 状压DP
		
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
 - 【BZOJ2073】[POI2004]PRZ 状压DP
		
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
 - bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
		
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
 - HDU 1074 Doing Homework (状压dp)
		
题意:给你N(<=15)个作业,每个作业有最晚提交时间与需要做的时间,每次只能做一个作业,每个作业超出最晚提交时间一天扣一分 求出扣的最小分数,并输出做作业的顺序.如果有多个最小分数一样的话,则 ...
 - 【BZOJ1688】[Usaco2005 Open]Disease Manangement 疾病管理 状压DP
		
[BZOJ1688][Usaco2005 Open]Disease Manangement 疾病管理 Description Alas! A set of D (1 <= D <= 15) ...
 - 【BZOJ1725】[Usaco2006 Nov]Corn Fields牧场的安排 状压DP
		
[BZOJ1725][Usaco2006 Nov]Corn Fields牧场的安排 Description Farmer John新买了一块长方形的牧场,这块牧场被划分成M列N行(1<=M< ...
 
随机推荐
- Java——文本组件:JTextComponent
			
import java.awt.GridLayout; import javax.swing.JFrame; import javax.swing.JLabel; import javax.swing ...
 - (转)JS Date格式化为yyyy-MM-dd类字符串
			
Date.prototype.format = function(format){ var o = { "M+" : this.getMonth()+1, //month &quo ...
 - Tween + 缓动函数
			
Unity-Tween http://www.cnblogs.com/MrZivChu/p/UnityTween.html iTween: iTween大解构(一)之抛物线移动 http://blog ...
 - 解决 linux下编译make文件报错“/bin/bash^M: 坏的解释器:没有那个文件或目录” 问题
			
PS背景:我在公司做sdk 的pc端开发,所以经常会在win下编译通过之后跑到linux下再运行一次已确保能支持多平台. 今儿在win下跑完一程序,然后放到linux下跑的时候,我用指令: [plai ...
 - ApiHelper
			
public static class ApiHelper { public static ReslultInfo<K> Try<K>(Action<ReslultInf ...
 - EntityFramework系列:SQLite.CodeFirst自动生成数据库
			
http://www.cnblogs.com/easygame/p/4447457.html 在Code First模式下使用SQLite一直存在不能自动生成数据库的问题,使用SQL Server C ...
 - Effective Objective-C 2.0 — 第三条:多用字面量语法,少用与之等价的方法
			
第三条:多用字面量语法,少用与之等价的方法 几个类:NSString NSNumber NSArray NSDictionary 字面量语法是一种语法糖(syntactic sugar) NSS ...
 - PHP使用数据库的并发问题(转)
			
在并行系统中并发问题永远不可忽视.尽管PHP语言原生没有提供多线程机制,那并不意味着所有的操作都是线程安全的.尤其是在操作诸如订单.支付等业务系统中,更需要注意操作数据库的并发问题. 接下来我通过一个 ...
 - 【转】Kafka实战-Flume到Kafka
			
Kafka实战-Flume到Kafka Kafka 2015-07-03 08:46:24 发布 您的评价: 0.0 收藏 2收藏 1.概述 前面给大家介绍了整个Kafka ...
 - 关于精简安装office2010的步骤
			
首先我们安装系统都会安装一个办公套件:office当然你也可以选择使用wps但我个人比较讨厌wps的广告 然而使用office完整安装不仅消耗资源大启动速度慢,而且一些功能我都用不到,所以我这几来一个 ...