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

Problem Description
ztr love reserach substring.Today ,he has n string.Now ztr want to konw,can he take out exactly k palindrome from all substring of these n string,and thrn sum of length of these k substring is L.



for example string "yjqqaq"

this string contains plalindromes:"y","j","q","a","q","qq","qaq".

so we can choose "qq" and "qaq".
 
Input
The first line of input contains an positive integer T(T<=10) indicating
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.
 
Output
For each test,Output a line.If can output "True",else output "False".
 
Sample Input
3
2 3 7
yjqqaq
claris
2 2 7
popoqqq
fwwf
1 3 3
aaa
 
Sample Output
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(回文串加多重背包)的更多相关文章

  1. hdu_5677_ztr loves substring(回文+二维多重背包)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5677 题意:给你N个串,问能否选出小于K个回文字符串使得选出的字符串的长度之和为L. 题解:很容易想到 ...

  2. HDU 5677 ztr loves substring(Manacher+dp+二进制分解)

    题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...

  3. HDU 5677 ztr loves substring

    Manacher+二维费用多重背包 二进制优化 这题是一眼标算....先计算出每个长度的回文串有几种,然后用二维费用的多重背包判断是否有解. 多重背包做的时候需要二进制优化. #include< ...

  4. hdu 5677 ztr loves substring 多重背包

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...

  5. Girls' research - HDU 3294 (Manacher处理回文串)

    题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...

  6. Harry and magic string HDU - 5157 记录不相交的回文串对数

    题意: 记录不相交的回文串对数 题解: 正着反着都来一遍回文树 用sum1[i] 表示到 i 位置,出现的回文串个数的前缀和 sun2[i]表示反着的个数 ans+=sum1[i-1]*sum2[i] ...

  7. HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)

    CA loves strings, especially loves the palindrome strings. One day he gets a string, he wants to kno ...

  8. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

随机推荐

  1. 使用typescript开发js代码提升代码维护性

    function test() { var gs = new Greeter('ss'); alert( gs.greet()); } window.onload = test; class Gree ...

  2. 【转载】XGBoost调参

    General Parameters: Guide the overall functioning Booster Parameters: Guide the individual booster ( ...

  3. Application Request Route实现IIS Server Farms集群负载详解(转)

    http://www.cnblogs.com/knowledgesea/p/5099893.html http://www.cnblogs.com/smileberry/p/4300849.html

  4. WCF实现客户端和服务端

    service side 1.定义ServiceContract: 2.new a ServiceHost 3. add endpoint using System.ServiceModel; nam ...

  5. unity 已知cosA和sinA,求A

    和c++中的atan2(y,x)类似,unity中有也Mathf.Atan2(y,x).

  6. AutoHotKey入门

    首先它要编译.ahk后缀的脚本才能执行.脚本里再写键盘触发监听之类的逻辑. 所以并非单单只是热键启动那么简单,可以组合出复杂的功能,甚至支持正则表达式 理论上扩展性比按键精灵差,易用性大大优于按键精灵 ...

  7. 使用spring的多线程机制

    多线程并发处理起来通常比較麻烦,假设你使用spring容器来管理业务bean,事情就好办了多了.spring封装了java的多线程的实现,你仅仅须要关注于并发事物的流程以及一些并发负载量等特性. 详细 ...

  8. 关于Tensorflow 的数据读取环节

    Tensorflow读取数据的一般方式有下面3种: preloaded直接创建变量:在tensorflow定义图的过程中,创建常量或变量来存储数据 feed:在运行程序时,通过feed_dict传入数 ...

  9. Powershell对象选择,排序和变量存储

    PowerShell基础教程(17)——对象的选择.排序和变量存储 可以使用 Select-Object cmdlet 来创建新的.自定义的 Windows PowerShell 对象,后者包含的属性 ...

  10. org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class

    转载自:http://songjianyong.iteye.com/blog/1663170 org.springframework.transaction.CannotCreateTransacti ...