ztr loves substring

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

Total Submission(s): 219    Accepted Submission(s): 119

Problem Description
ztr love reserach substring.Today ,he has n string.Now ztr want to konw,can he take out exactly k palindrome from all substring of these n string,and thrn sum of length of these k substring is L.



for example string "yjqqaq"

this string contains plalindromes:"y","j","q","a","q","qq","qaq".

so we can choose "qq" and "qaq".
 
Input
The first line of input contains an positive integer T(T<=10) indicating
the number of test cases.



For each test case:



First line contains these positive integer N(1<=N<=100),K(1<=K<=100),L(L<=100).

The next N line,each line contains a string only contains lowercase.Guarantee even length of string won't more than L.
 
Output
For each test,Output a line.If can output "True",else output "False".
 
Sample Input
  1. 3
  2. 2 3 7
  3. yjqqaq
  4. claris
  5. 2 2 7
  6. popoqqq
  7. fwwf
  8. 1 3 3
  9. aaa
 
Sample Output
  1. False
  2. True
  3. True
  4.  
  5. 由于字符串的长度只有100,所以我们可以用暴力求所有的回文子串,可以动态规划去求。
  6. 然后就可以用多重二维费用背包来处理,这里不需要用单调队列优化,不会超时,另外如果K>L直接是False
  7.  
  8. #include <iostream>
  9. #include <string.h>
  10. #include <stdlib.h>
  11. #include <algorithm>
  12. #include <stdio.h>
  13. #include <math.h>
  14. using namespace std;
  15. int dp[105][105];
  16. int bp[105][105];
  17. int v[105];
  18. int c[105];
  19. char a[105];
  20. int n,K,L;
  21. void OneZeroPack(int v,int w)
  22. {
  23.     for(int i=K;i>=1;i--)
  24.     {
  25.         for(int j=L;j>=v;j--)
  26.         {
  27.             bp[i][j]=max(bp[i][j],bp[i-1][j-v]+w);
  28.         }
  29.     }
  30. }
  31. void CompletePack(int v,int w)
  32. {
  33.     for(int i=1;i<=K;i++)
  34.     {
  35.         for(int j=v;j<=L;j++)
  36.         {
  37.             bp[i][j]=max(bp[i][j],bp[i-1][j-v]+w);
  38.         }
  39.     }
  40. }
  41. void MulitplyPack(int v,int w,int c)
  42. {
  43.     if(c*w>=L)
  44.     {
  45.         CompletePack(v,w);
  46.         return;
  47.     }
  48.     int k=1;
  49.     while(k<c)
  50.     {
  51.        OneZeroPack(k*v,k*w);
  52.         c-=k;
  53.         k<<=1;
  54.     }
  55.     OneZeroPack(c*v,c*w);
  56. }
  57. int main()
  58. {
  59.     int t;
  60.     scanf("%d",&t);
  61.     int m;
  62.     while(t--)
  63.     {
  64.         scanf("%d%d%d",&n,&K,&L);
  65.         m=0;
  66.         memset(c,0,sizeof(c));
  67.         memset(v,0,sizeof(v));
  68.         v[0]=1;
  69.         for(int i=1;i<=n;i++)
  70.         {
  71.             memset(dp,0,sizeof(dp));
  72.             for(int p=1;p<=100;p++)
  73.                 dp[p][p]=1;
  74.             scanf("%s",a);
  75.             int len=strlen(a);
  76.             m=max(m,len);
  77.             c[0]+=len;
  78.             for(int l=1;l<=len-1;l++)
  79.             {
  80.                 int num=0;
  81.                 for(int i=0;i+l<=len-1;i++)
  82.                 {
  83.                     int j=i+l;
  84.                     if(a[i]==a[j]&&(j-i==1||dp[i+1][j-1]==1))
  85.                     {
  86.                         dp[i][j]=1;
  87.                         num++;
  88.                     }
  89.                 }
  90.                 v[l]=l+1;
  91.                 c[l]+=num;
  92.             }
  93.         }
  94.         memset(bp,0,sizeof(bp));
  95.         for(int i=0;i<m;i++)
  96.         {
  97.             if(c[i]==0) continue;
  98.             MulitplyPack(v[i],v[i],c[i]);
  99.         }
  100.         if(K>L)
  101.         {
  102.             printf("False\n");
  103.             continue;
  104.         }
  105.         if(bp[K][L]==L)
  106.             printf("True\n");
  107.         else
  108.             printf("False\n");
  109.     }
  110.     return 0;
  111. }
 

HDU 5677 ztr loves substring(回文串加多重背包)的更多相关文章

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

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=5677 题意:给你N个串,问能否选出小于K个回文字符串使得选出的字符串的长度之和为L. 题解:很容易想到 ...

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

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

  3. HDU 5677 ztr loves substring

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

  4. hdu 5677 ztr loves substring 多重背包

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

  5. Girls' research - HDU 3294 (Manacher处理回文串)

    题目大意:给以一个字符串,求出来这个字符串的最长回文串,不过这个字符串不是原串,而是转换过的,转换的原则就是先给一个字符 例如 'b' 意思就是字符把字符b转换成字符 a,那么c->b, d-& ...

  6. Harry and magic string HDU - 5157 记录不相交的回文串对数

    题意: 记录不相交的回文串对数 题解: 正着反着都来一遍回文树 用sum1[i] 表示到 i 位置,出现的回文串个数的前缀和 sun2[i]表示反着的个数 ans+=sum1[i-1]*sum2[i] ...

  7. HDU5658:CA Loves Palindromic (回文树,求区间本质不同的回文串数)

    CA loves strings, especially loves the palindrome strings. One day he gets a string, he wants to kno ...

  8. [LeetCode] Longest Palindromic Substring 最长回文串

    Given a string S, find the longest palindromic substring in S. You may assume that the maximum lengt ...

  9. HDU 5651 计算回文串个数问题(有重复的全排列、乘法逆元、费马小定理)

    原题: http://acm.hdu.edu.cn/showproblem.php?pid=5651 很容易看出来的是,如果一个字符串中,多于一个字母出现奇数次,则该字符串无法形成回文串,因为不能删减 ...

随机推荐

  1. JiaThis™“分享到”侧栏代码

    风格:迷你: 猜你喜欢: 开启 使用说明: 复制并粘贴下面的JS代码,放到您的网页,可以在<body>和</body>的之间网页的任意位置放置.如果您的网站使用的模板,     ...

  2. Rabbitmq消息队列(四) 发布订阅

    1.简介 在上篇教程中,我们搭建了一个工作队列,每个任务只分发给一个工作者,在本篇教程中,我们要做的跟之前完全不一样 —— 分发一个消息给多个消费者(consumers).这种模式被称为“发布/订阅” ...

  3. object-c全局变量

    跟c++一定,在.m里Obj*obj=NULL,在.h里extern Obj*obj 即可.

  4. AspNet GridView Excel 下载 Excel 导出

    1.GridView AutoGenerateColums =false; DataSource DataBind 2.BoundField DataField HeaderText ItemStyl ...

  5. EF检索中文失败的解决办法

    1. MYSQL: 保证所有的的列都是UTF8格式. 2. VS2010: 在data server建立连接时,选择advance,将chracterset设成utf8,这样在VS2010里查看和更改 ...

  6. (WPF)依赖属性

    属性触发器: <Button MinWidth=" 75" Margin="10"> <Button.Style> <Style ...

  7. 记一次kafka客户端NOT_COORDINATOR_FOR_GROUP处理过程

    转发请注明原创地址:https://www.cnblogs.com/dongxiao-yang/p/10602799.html 某日晚高峰忽然集群某个大流量业务收到lag报警,查看客户端日志发现reb ...

  8. Oracle之函数concat、lpad

    一.引言 程序测试需要生成大量的测试数据,且测试数据有主键,主键自增,于是决定用存储过程来实现,经过半天的查资料终于完成了,记录之,学习之 二.存储过程 格式: CREATE PROCEDURE re ...

  9. QT .pro文件 LIBS用法详解

    在程序中需要使用到团队其它成员开发的静态库和动态库,起初是知道使用LIBS变量在在.pro文件中指定需要包含的库,但是实际使用的时候却遇到很大麻烦,但其实确实是因为自己看官方文档不太用心造成的. 下面 ...

  10. HTML5七巧板canvas绘图(复习)

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...