HDU 5677 ztr loves substring(Manacher+dp+二进制分解)
题目链接:HDU 5677 ztr loves substring
题意:有n个字符串,任选k个回文子串,问其长度之和能否等于L。
题解:用manacher算法求出所有回文子串的长度,并记录各长度回文子串的个数,再用背包思想判断是否有解。
dp[i][j]:选取i个回文子串,长度之和是否为j
其中用到二进制分解思想,将回文串长为i的数量cnt[i]拆成1+2+4+8+…形式,进行优化。
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
#define CLR(a,b) memset((a),(b),sizeof((a)))
const int N = ;
int dp[N][N], w[N*N*N], cnt[N], cnt1[N*N*N];
char s[N*];
void Manacher(char s[],int len) {
char Ma[N*];
int Mp[N*] , l = ;
CLR(Mp, );
Ma[l++] = '$'; Ma[l++] = '#';
for(int i = ; i < len; i++) {
Ma[l++] = s[i]; Ma[l++] = '#';
}
int mx = , id = ;
for(int i = ;i < l; i++) {
Mp[i] = mx > i ? min(Mp[*id-i], mx-i) : ;
while(Ma[i+Mp[i]] == Ma[i-Mp[i]]) Mp[i]++;
if(i + Mp[i] > mx) {
id = i;
mx = i + Mp[i];
}
if(Ma[i] == '#'&& Mp[i] == ) continue;
cnt[Mp[i]-]++;//记录该回文串长度数量
}
}
int main(){
int t, i, j, n, k, l, x, num;
scanf("%d", &t);
while(t--) {
scanf("%d%d%d", &n, &k, &l);
CLR(dp, ); CLR(cnt, );
for(i = ; i < n; ++i) {
scanf("%s", s);
int len = strlen(s);
Manacher(s, len);
}
num = ;
for(i = ; i <= ; ++i) {
for(j = ; j <= cnt[i]; cnt[i] -= j, j <<= ) {
w[num] = j * i;
cnt1[num++] = j;
}
if(cnt[i]) { w[num] = j * i; cnt1[num++] = j; }
}
dp[][] = ;
for(i = ; i < num; ++i)
for(j = l; j >= w[i]; --j)
for(x = cnt1[i]; x <= k; ++x)
dp[x][j] |= dp[x-cnt1[i]][j-w[i]];
if(dp[k][l]) puts("True");
else puts("False");
}
return ;
}
0ms
HDU 5677 ztr loves substring(Manacher+dp+二进制分解)的更多相关文章
- 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( ...
- HDU 5677 ztr loves substring
Manacher+二维费用多重背包 二进制优化 这题是一眼标算....先计算出每个长度的回文串有几种,然后用二维费用的多重背包判断是否有解. 多重背包做的时候需要二进制优化. #include< ...
- [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 ...
随机推荐
- 使用rem单位的问题——Google下字体很大
rem的看法 rem单位确实好处蛮多的,它是相对于根节点,让我们整个网站单位可以统一.还可以让我们的字体更好的自适应网站的大小.但是,你用过了就知道,它会出现一个问题: 用Chrome浏览器打开你做的 ...
- mysql 优化常用语句
show status;//查询mysql各种状态: show variables like 'long_query_time';//慢查询的限定时间 set long_query_time=1;// ...
- [PY3]——内置数据结构(8)——解构与封装
### 解构的理解与用法 ### 解构是python很有特色的一个功能,被很多语言借鉴(例如ES6) # 元素按照顺序赋值给变量 In [31]: lst=list(range(5)) In [32] ...
- linux新建用户切换后显示-bash-4.1$(转载)
今天新建了一个MQM的用户 , [plain] view plaincopy useradd -g mqm -d /var/mqm passwd mqm 终端中显示 -bash-4.1$而不是 [mq ...
- angular 与 layer 集成过程
layer 的提示框和弹层确实也好用,在使用angular的前提下,使用layer遇到诸多麻烦,记录下来. 在类型是1页面层,主要问题在遮罩方面,造成无法编辑. 开始:引入layer 样式,angul ...
- awk中引用变量使用单引号''
举例如下 who命令输出第一列 (1)第一种情况不使用引号 # i=1;who | awk '{print $${i}}' 输出如下: awk: {print $${i}} awk: ...
- SQL Server 2008中的MERGE(数据同步)
OK,就像标题呈现的一样,SQL Server 2008中的MERGE语句能做很多事情,它的功能是根据源表对目标表执行插入.更新或删除操作.最典型的应用就是进行两个表的同步. 下面通过一个简单示例来演 ...
- Autofac +webapi 配置
Autofac配置 using Autofac; using System; using System.Collections.Generic; using System.Linq; using Sy ...
- Tomcat服务器使用(二)
1. 打包Javaweb应用 当开发人员在自己的开发机器上调试所有代码并通过后,为了进行产品发布,都需要将开发人员的源码打包成war包进行发布.之前已经了解了jar包,那么war包和jar包的区别是什 ...
- 五、spring之DI循环依赖
什么是循环依赖 循环依赖就是循环引用,就是两个或多个Bean相互之间的持有对方,比如CircleA引用CircleB,CircleB引用CircleC,CircleC引用CircleA,则它们最终反映 ...