HDU 5677 ztr loves substring
Manacher+二维费用多重背包 二进制优化
这题是一眼标算....先计算出每个长度的回文串有几种,然后用二维费用的多重背包判断是否有解。
多重背包做的时候需要二进制优化。
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std; const int maxn = ;
int N, p[maxn];
char str[maxn], b[maxn];
int cnt[maxn];
int n, k, L;
bool dp[maxn][maxn]; void init()
{
int i;
for (i = ; str[i]; i++) b[ * i + ] = '#', b[ * i + ] = str[i];
N = * i + ;
b[] = '$', b[N] = b[N + ] = '#';
} void solve()
{
int i, id, max = ;
for (i = ; i <= N; i++)
{
p[i] = i < max ? std::min(max - i, p[ * id - i]) : ;
while (b[i + p[i]] == b[i - p[i]]) ++p[i];
if (i + p[i] > max) max = i + p[i], id = i;
cnt[p[i] - ]++;
}
}
int main()
{
int T; scanf("%d", &T);
while (T--)
{
memset(dp, , sizeof dp); dp[][] = ;
memset(cnt, , sizeof cnt);
scanf("%d%d%d", &n, &k, &L);
while (n--){ scanf("%s", str); init(); solve(); }
for (int i = ; i >= ; i--)
cnt[i] = cnt[i] + cnt[i + ];
for (int i = ; i >= ; i--)
{
if (cnt[i] == ) continue;
int val = i, num = cnt[i];
int t = ;
while (num)
{
if (num > t)
{
int tmp_val = val*t;
for (int d = L; d >= ; d--)
{
for (int f = k; f >= ; f--)
{
if (dp[d][f] == ) continue;
if (d + tmp_val <= L&&f + t <= k)
dp[d + tmp_val][f + t] = ;
}
}
num = num - t;
t = t * ;
}
else
{
int tmp_val = val*num;
for (int d = L; d >= ; d--)
{
for (int f = k; f >= ; f--)
{
if (dp[d][f] == ) continue;
if (d + tmp_val <= L&&f + num <= k)
dp[d + tmp_val][f + num] = ;
}
}
num = ;
}
}
}
if (dp[L][k]) printf("True\n");
else printf("False\n");
}
return ;
}
HDU 5677 ztr loves substring的更多相关文章
- HDU 5677 ztr loves substring(Manacher+dp+二进制分解)
题目链接:HDU 5677 ztr loves substring 题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L. 题解:用manacher算法求出所有回文子串的长度,并记录各长度回文 ...
- HDU 5677 ztr loves substring(回文串加多重背包)
ztr loves substring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- hdu 5677 ztr loves substring 多重背包
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission( ...
- [HDU5677]ztr loves substring
ztr loves substring Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Othe ...
- HDU 5675 ztr loves math (数学推导)
ztr loves math 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/A Description ztr loves re ...
- HDU 5676 ztr loves lucky numbers (模拟)
ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...
- HDU 5675 ztr loves math
ztr loves math Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)To ...
- hdu 5676 ztr loves lucky numbers(dfs+离线)
Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...
- hdu 5675 ztr loves math(数学技巧)
Problem Description ztr loves research Math.One day,He thought about the "Lower Edition" o ...
随机推荐
- ComboBox绑定Dictionary做为数据源
http://www.cnblogs.com/refresh/archive/2012/07/14/2591503.html https://msdn.microsoft.com/zh-cn/libr ...
- c++11 右值引用、move、完美转发forward<T>
#include <iostream> #include <string> using namespace std; template <typename T> v ...
- inserted触发器,一张表插入数据时,同时向另外一张表插入数据
有时候,一个服务器上有多个数据库,需要向其中一个数据库的表中插入数据时, 同时向另外一个数据的表里插入数据. 可以利用触发器和同义词(建立同义词的方法省略), 在一个数据库的表里插入数据时,同时向另外 ...
- Linux系统查看有几块硬盘
使用df命令即可查看.df 是来自于coreutils 软件包,系统安装时,就自带的:我们通过这个命令可以查看磁盘的使用情况以及文件系统被挂载的位置: 示例:[root@localhost ~]# d ...
- Hibernate 系列教程2-创建maven工程
第1步:通过eclipse新建1个java maven项目. 选择file–>new–>other–>MAVEN PROJECT选项 第2步:New Maven project 选择 ...
- struts2中的<s:select>默认选项
//... public class SelectAction extends ActionSupport{ private List<String> searchEngine; priv ...
- LoadRunner 技巧之协议分析(五)
在做性能测试的时候,协议分析是困扰初学者的难题,选择错误的协议会导致Virtual User Generator 录制不到脚本:或录制的脚本不完整,有些应用可能需要选择多个协议才能完整的记录 客户端与 ...
- 快学Scala-第九章 文件和正则表达式
知识点: 1.读取文件中的所有行,可以调用scala.io.Source对象的getLines方法: import scala.io.Source val source = Source.from(& ...
- SDAU课程练习--problemQ(1016)
题目描述 FJ is surveying his herd to find the most average cow. He wants to know how much milk this 'med ...
- 当使用System,out.println()打印一个对象是自动调用toString方法
在Java中,所有的对象都是继承自Object,自然继承了toString方法,在当使用System,out.println()里面为一个对象的引用时,自动调用toString方法讲对象打印出来.如果 ...