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 ...
随机推荐
- ccw-ide
有bug,会把working set弄乱,整理后能重启一次正常,再次重启又乱了.
- mynotebook
www.linux.org/threads/beats-audio-on-linux.4443/ askubuntu.com/questions/303775/envy-15-beats-audio- ...
- Hibernate 系列教程12-继承-Join策略
Employee public class Employee { private Long id; private String name; HourlyEmployee public class H ...
- UITabBarItem's appearance
1.我们知道,用tabBarController创建出来默认的tabBar似这个样子滴... -----------------我是图片分割线----------------------------- ...
- Struts2龙之总结
一.Struts2执行流程: 1.客户端初始化一个指向servlet容器(tomcat)的请求: 2.这个请求经过一系列过滤器(Filter): 3.接着StrutsPrepareAndExecute ...
- svn 设置文件可执行权限
本地文件在commit到仓库之前若没有chmod +x 权限的话,那在svn仓库里的文件将会保持当前无可执行属性状态. 即使在本地chmod +x filename 之后,再提交到仓库也是没有用的.c ...
- UVALive 2521 Game Prediction 题解
这个我上来把题目理解错了,我以为所有人的牌都是一样的,感觉这个题太麻烦了吧,而且题目样例过不去啊……后来发现理解错了,给出的数据是他一个人的数据,就是让我们求他一定能赢的轮数,所有的牌是固定的(1 - ...
- 九宫格问题 A*
八数码问题 一.八数码问题八数码问题也称为九宫问题.在3×3的棋盘,摆有八个棋子,每个棋子上标有1至8的某一数字,不同棋子上标的数字不相同.棋盘上还有一个空格,与空格相邻的棋子可以移到空格中.要求解决 ...
- heartbeat集群安装配置
安装配置高可用集群需要注意:1.节点名称:集群每个节点的名称都得能互相解析 /etc/hosts hosts主机名的正反解析结果必须跟"uname -n"的结果保持一致2.时间必须 ...
- [LeetCode] Magical String 神奇字符串
A magical string S consists of only '1' and '2' and obeys the following rules: The string S is magic ...