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 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...
随机推荐
- angularJS使用内置服务
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <script sr ...
- oracle expdp导入时 提示“ORA-39002: 操作无效 ORA-39070: 无法打开日志文件 ”
1.导出数据库的时候报错 expdp zz/zz@orcl directory=exp_dp dumpfile=zz_20170520.dump logfile=zz_20170520.log 2 ...
- GNU--gprof使用总结
Added macros ACE_USES_GPROF which enables users to use gprof in a multithreaded environment with ACE ...
- UIPopoverController具体解释
今天一位童鞋问我个问题.大意是popoverController不会显示.经过我寻找问题发现以下这种方法不好掌控. 为什么说他不好掌控那.我这个给大家带来一个列子.通过这个列子来介绍PopoverCo ...
- sklearn 随机森林方法
Notes The default values for the parameters controlling the size of the trees (e.g. max_depth, min_s ...
- CORS解决WebApi跨域问题(转)
CORS全称Cross-Origin Resource Sharing,中文全称跨域资源共享.它解决跨域问题的原理是通过向http的请求报文和响应报文里面加入相应的标识告诉浏览器它能访问哪些域名的请求 ...
- unity, GetComponent<Renderer>().bounds.size vs GetComponent<MeshFilter>().sharedMesh.bounds.size
GetComponent<MeshFilter>().sharedMesh.bounds.size获得的是未经缩放的大小. GetComponent<Renderer>().b ...
- Atitit.js模块化 atiImport 的新特性javascript import
Atitit.js模块化 atiImport 的新特性javascript import 1. 常见的js import规范amd ,cmd ,umd1 1.1. Require更多流行3 2. at ...
- Binary Tree Postorder Traversal --leetcode
原题链接:https://oj.leetcode.com/problems/binary-tree-postorder-traversal/ 题目大意:后序遍历二叉树 解题思路:后序遍历二叉树的步骤: ...
- javaweb 压缩文件图片
压缩图片分辨率 ,根据 url 和 根据 file /** * <p>Title: thumbnailImage</p> * <p>Description: 根据 ...