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的更多相关文章

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

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

  2. HDU 5677 ztr loves substring(回文串加多重背包)

    ztr loves substring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  3. hdu 5677 ztr loves substring 多重背包

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

  4. [HDU5677]ztr loves substring

    ztr loves substring Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  5. HDU 5675 ztr loves math (数学推导)

    ztr loves math 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/A Description ztr loves re ...

  6. HDU 5676 ztr loves lucky numbers (模拟)

    ztr loves lucky numbers 题目链接: http://acm.hust.edu.cn/vjudge/contest/121332#problem/I Description ztr ...

  7. HDU 5675 ztr loves math

    ztr loves math Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)To ...

  8. hdu 5676 ztr loves lucky numbers(dfs+离线)

    Problem Description ztr loves lucky numbers. Everybody knows that positive integers are lucky if the ...

  9. hdu 5675 ztr loves math(数学技巧)

    Problem Description ztr loves research Math.One day,He thought about the "Lower Edition" o ...

随机推荐

  1. 报表Reporting S而vice是 错误的解决

    Reporting Services 错误 报表服务器无法打开与报表服务器数据库的连接.所有请求和处理都要求与数据库建立连接. (rsReportServerDatabaseUnavailable)获 ...

  2. 笨方法学python--打印

    1 print 语句 尾端的逗号 end1 = "C" end2 = "h" end3 = "e" end4 = "e" ...

  3. nginx+php-fpm 的配置下,php的错误日志

    发现php 运行错误时,浏览器的网页上并没有输出php的错误日志.那php的错误日志在哪里呢? 发现在 /var/log/nginx/error.log文件中. 怎么样才能在浏览器的网页中输出php的 ...

  4. 多线程junit单元测试

    junit中测试完成后会进行jvm退出,而不是线程退出,所以任一线程退出都会导致测试结束,junit想进行多线程测试需要进行另外包装,网上看到一个投机取巧的例子还不错,贴上我的测试代码(代码中我需要测 ...

  5. table边框设置

    一.表格的常用属性基本属性有:width(宽度).height(高度).border(边框值).cellspacing(表格的内宽,即表格与tr之间的间隔). cellpadding(表格内元素的间隔 ...

  6. HDU 2844 Coins 背包问题 + 二进制优化

    题目大意:某个人有n种硬币,每种硬币价值为v,数量为c,问在总价值不超过m的条件下,最多有多少种组合方式. 题目思路: 1.对于某种硬币 如果v*c 大于 m,就意味着无论取多少枚硬币,只要总价值不大 ...

  7. Debian 安装Nvidia显卡驱动

    1.到nvidia官方网站下载自己显卡对应型号得驱动,如果不知道显卡型号,可以使用如下命令来查看 lspci | grep VGA 2.安装显卡驱动所必需得工具 apt-get install bui ...

  8. Java Networking Related (Java Examples in a Nutshell 3rd Edition)

    Examples to: Use URL class to parse URLs and download the network resources specified by a URL Use U ...

  9. k-Means和KNN算法简述

    k-means 算法 k-means 算法接受输入量 k :然后将n个数据对象划分为 k个聚类以便使得所获得的聚类满足:同一聚类中的对象相似度较高:而不同聚类中的对象相似度较小.聚类相似度是利用各聚类 ...

  10. 【啊哈!算法】算法7:Dijkstra最短路算法

    上周我们介绍了神奇的只有五行的Floyd最短路算法,它可以方便的求得任意两点的最短路径,这称为“多源最短路”.本周来来介绍指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径”.例如求下图 ...