首页 技术 正文
技术 2022年11月12日
0 收藏 500 点赞 5,031 浏览 1145 个字

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5677

题意:给你N个串,问能否选出小于K个回文字符串使得选出的字符串的长度之和为L。

题解:很容易想到求一下回文字符串的个数和长度,然后就背包处理一下,数据比较水,用了manacher和二进制背包加速,0ms过。

hdu_5677_ztr loves substring(回文+二维多重背包)

 #include<cstdio>
#include<cstring>
#define min(a,b) (a)>(b)?(b):(a)
const int maxn = ;//字符串长度
char s[];
int bk[],t,n,k,l,val[],size[];
bool dp[][];
struct Manacher{
char str[maxn<<];
int p[maxn<<],len,mx,id,tl,ans,i;
void maxlen(char *s){
len=strlen(s),mx=,id=,tl=,str[tl++]='$',str[tl++]='#';
for(i=;i<len;i++)str[tl++]=s[i],str[tl++]='#';
for(i=,str[tl]=,ans=;i<tl;i++){
p[i]=mx>i?min(p[(id<<)-i],mx-i):;
while(str[i-p[i]]==str[i+p[i]])p[i]++;
if(i+p[i]>mx)mx=i+p[i],id=i;
if(str[i]=='#'&&p[i]==)continue;
bk[p[i]-]++;
}
}
}M;
void back(){
int cnt=;//将所有的回文串二进制拆解
for(int i=;i<=;i++){
int tmp=;
while(bk[i]>=tmp)
val[++cnt]=tmp*i,size[cnt]=tmp,bk[i]-=tmp,tmp*=;
if(bk[i])val[++cnt]=tmp*i,size[cnt]=tmp;
}
memset(dp,,sizeof(dp));
dp[][]=;
for(int i=;i<=cnt;i++){
for(int j=k;j>=size[i];j--)
for(int r=l;r>=val[i];r--)
if(dp[j-size[i]][r-val[i]])dp[j][r]=;
}
}
int main(){
scanf("%d",&t);
while(t--){
scanf("%d%d%d",&n,&k,&l);
memset(bk,,sizeof(bk));
for(int i=;i<=n;i++){scanf("%s",s);M.maxlen(s);}
back();
if(dp[k][l])puts("True");
else puts("False");
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289