题目链接: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+二进制分解)的更多相关文章

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

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

  2. hdu 5677 ztr loves substring 多重背包

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

  3. HDU 5677 ztr loves substring

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

  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. Xcode日常使用

    1.构建输出目录(Preferences->Locations->Locations)当选择为Default时,Derived Data的目录为~/Library/Developer/Xc ...

  2. 项目管理系列--好用的代码评审(Code Review)工具

    1. Gerrit Gerrit is a web based code review system, facilitating online code reviews for projects us ...

  3. NOPI 导出 Excel 2007

    代码: public static void ThisTo<T>( List<T> source, string[] colums, Func<T, object[]&g ...

  4. 阿里云配置HTTPS

    阿里云配置HTTPS 2018-05-29 16:00:56 by SemiconductorKING 最近申请域名配置了一下HTTPS协议,记录一下配置过程. 准备 备案过的域名,一个SSL证书(免 ...

  5. D3基础---比例尺

    转载请注明出处! 比例尺简述: 比例尺是一组把输入域映射到输出范围的函数. 一般来说数据集中的值不可能恰好与图表中的像素尺度一一对应.比例尺就是把这些数据值映射到可视化图形中使用的新值的便捷手段. D ...

  6. .netcore2.0 有关配置

    1.在部署WebApi 或者网站时常用的2个配置数据库连接字符串.绑定Url地址 2.# 数据库连接字符串配置: 默认的配置文件 appsettings.json 添加配置节点: "Conn ...

  7. Nginx 502错误总结

    http请求流程:一般情况下,提交动态请求的时候,nginx会直接把 请求转交给php-fpm,而php-fpm再分配php-cgi进程来处理相关的请求,之后再依次返回,最后由nginx把结果反馈给客 ...

  8. RecyclerView IndexOutOfBoundsException 问题

    在项目中遇到一个RecyclerView 偶现的奔溃,查看日志,发现是: java.lang.IndexOutOfBoundsException: Index: 39, Size: 39 at jav ...

  9. spring和jdbc结合的一个小例子

    1.新建一个SpringJdbc的maven项目. 2.引入需要的jar包的依赖 <span style="white-space:pre"> </span> ...

  10. webpack-loader原理

    loader loader 是导出为一个函数的 node 模块.该函数在 loader 转换资源的时候调用.给定的函数将调用 loader API,并通过 this 上下文访问. loader配置 { ...