HDU 5677 ztr loves substring(回文串加多重背包)
ztr loves substring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 219 Accepted Submission(s): 119
for example string "yjqqaq"
this string contains plalindromes:"y","j","q","a","q","qq","qaq".
so we can choose "qq" and "qaq".
the number of test cases.
For each test case:
First line contains these positive integer N(1<=N<=100),K(1<=K<=100),L(L<=100).
The next N line,each line contains a string only contains lowercase.Guarantee even length of string won't more than L.
3
2 3 7
yjqqaq
claris
2 2 7
popoqqq
fwwf
1 3 3
aaa
False
True
True由于字符串的长度只有100,所以我们可以用暴力求所有的回文子串,可以动态规划去求。然后就可以用多重二维费用背包来处理,这里不需要用单调队列优化,不会超时,另外如果K>L直接是False#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <stdio.h>
#include <math.h> using namespace std;
int dp[105][105];
int bp[105][105];
int v[105];
int c[105];
char a[105];
int n,K,L;
void OneZeroPack(int v,int w)
{
for(int i=K;i>=1;i--)
{
for(int j=L;j>=v;j--)
{
bp[i][j]=max(bp[i][j],bp[i-1][j-v]+w);
}
}
}
void CompletePack(int v,int w)
{
for(int i=1;i<=K;i++)
{
for(int j=v;j<=L;j++)
{
bp[i][j]=max(bp[i][j],bp[i-1][j-v]+w);
}
}
}
void MulitplyPack(int v,int w,int c)
{
if(c*w>=L)
{
CompletePack(v,w);
return;
}
int k=1;
while(k<c)
{
OneZeroPack(k*v,k*w);
c-=k;
k<<=1;
}
OneZeroPack(c*v,c*w);
}
int main()
{
int t;
scanf("%d",&t);
int m;
while(t--)
{
scanf("%d%d%d",&n,&K,&L);
m=0; memset(c,0,sizeof(c));
memset(v,0,sizeof(v));
v[0]=1;
for(int i=1;i<=n;i++)
{
memset(dp,0,sizeof(dp));
for(int p=1;p<=100;p++)
dp[p][p]=1;
scanf("%s",a);
int len=strlen(a);
m=max(m,len);
c[0]+=len;
for(int l=1;l<=len-1;l++)
{
int num=0;
for(int i=0;i+l<=len-1;i++)
{
int j=i+l;
if(a[i]==a[j]&&(j-i==1||dp[i+1][j-1]==1))
{
dp[i][j]=1;
num++;
}
}
v[l]=l+1;
c[l]+=num;
}
}
memset(bp,0,sizeof(bp));
for(int i=0;i<m;i++)
{
if(c[i]==0) continue;
MulitplyPack(v[i],v[i],c[i]);
}
if(K>L)
{
printf("False\n");
continue;
}
if(bp[K][L]==L)
printf("True\n");
else
printf("False\n"); }
return 0;
}
HDU 5677 ztr loves substring(回文串加多重背包)的更多相关文章
- hdu_5677_ztr loves substring(回文+二维多重背包)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5677 题意:给你N个串,问能否选出小于K个回文字符串使得选出的字符串的长度之和为L. 题解:很容易想到 ...
- HDU 5677 ztr loves substring(Manacher+dp+二进制分解)
题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...
- HDU 5677 ztr loves substring
Manacher+二维费用多重背包 二进制优化 这题是一眼标算....先计算出每个长度的回文串有几种,然后用二维费用的多重背包判断是否有解. 多重背包做的时候需要二进制优化. #include< ...
- hdu 5677 ztr loves substring 多重背包
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...
- Girls' research - HDU 3294 (Manacher处理回文串)
题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...
- Harry and magic string HDU - 5157 记录不相交的回文串对数
题意: 记录不相交的回文串对数 题解: 正着反着都来一遍回文树 用sum1[i] 表示到 i 位置,出现的回文串个数的前缀和 sun2[i]表示反着的个数 ans+=sum1[i-1]*sum2[i] ...
- HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)
CA loves strings, especially loves the palindrome strings. One day he gets a string, he wants to kno ...
- [LeetCode] Longest Palindromic Substring 最长回文串
Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...
- HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)
原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
随机推荐
- SSL and SSL Certificates Explained
Secure Sockets Layer (SSL) and Transport Layer security (TLS ) are protocols that provide secure com ...
- python-创建列表
创建个空列表 album = [] 创建非空列表 album = ['Black Star','David Bowie',25.True] 向列表中添加新的元素 append 方法,元素自动地排列到列 ...
- 【Objective-C】02-Objective-C学习及iOS开发的准备
前言 由于大部分朋友对iOS开发还是有比较多的疑问,比如是不是一定要买iPhone和苹果笔记本才能做iOS开发?因此,这讲就为广大有意学习iOS开发的朋友们进行解惑. 一.什么是iOS开发 iOS是由 ...
- [JNA系列]Java调用Delphi编写的Dll之实例Delphi使用PAnsiChar
Delphi代码 unit UnitDll; interface uses StrUtils, SysUtils, Dialogs; function DoBusinessAnsi(pvData: P ...
- PHP特性整合(PHP5.X到PHP7.1.x)
Buid-in web server内置了一个简单的Web服务器 把当前目录作为Root Document只需要这条命令即可: php -S localhost:3300 也可以指定其它路径 php ...
- Webdings 图形字体
如果想在网页上插入图形,最简单的方式就是使用图形字体.Webdings 是一种微软开发的图形字体,在IE浏览器上可以使用它. 什么是Webdings Webdings 是一个TrueType的ding ...
- C#非常规调试场景总结
场景1:类库独立调试. 方法:可以将类库项目修改成控制台程序,然后增加一个静态的main函数的方式来调试 场景2:程序需要连接数据库,本机调试的时候因为权限问题无法连接上数据库,只能 ...
- 最短路中部分点仅仅能从中随意选取K个问题
题意:给N个点,还有另外m个点(当中仅仅能选K个).求最短路. 思路:在SPFA的基础上,用一个数组来统计,在某点入队时(要拓展其它点了),若该点是m个点中的,则count[i]=原来的+1:若不是. ...
- linux查杀病毒的几个思路
1. tmp 目录下面 2. 定时任务查找一下 crontab -e 3. 病毒要下载 ps -ef|egrep "curl|wget" 查看是否有下载的命令 4. top 一 ...
- android viewpager嵌套使用photoview异常问题
最近,做项目时,遇到一个需求,需要像淘宝评论那样,一组图点开,然后可以双指滑动放大,并左右切换换图的功能.自然就想到了使用viewpager+photoview来实现这一功能,但是在实现后,却发现一个 ...