BestCoder Round #82 (div.1) 1002 HDU 5677 dp-类似多重背包的思想
来源:互联网 发布:淘宝东西会自动下架吗 编辑:程序博客网 时间:2024/05/18 03:02
链接:戳这里
ztr loves substring
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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
3
2 3 7
yjqqaq
claris
2 2 7
popoqqq
fwwf
1 3 3
aaa
Sample Output
False
True
True
题意:
给出n个串,从这n个串中选出K个回文子串,使得选出的K个回文子串的长度总和为L,满足条件输出True ,否则False
思路:
处理出长度为i的回文子串的个数(1<=i<=L) num[i]
设置dp状态:dp[i][j][k] 表示当前执行了i次取舍操作之后,能否达到选取j个回文子串,长度为k,能达到状态dp[i][j][k]=1,否则为0
由于每次设置的dp状态只跟上一层的有关 ,可以使用滚动数组,这样就消去了一维i
dp[2][i][j] 表示当前选了i个回文子串使得长度为j的状态能否达到,而当前的状态只需要通过上一层转移即可
那么我们再来分析怎么实现dp状态的转移
1:dp[now][0][0]=1
2:for(1<=i<=L) 枚举长度为i的回文子串去计算贡献
3: for(1<=l<=L) 表示当前长度为l
4: for(1<=j<=K) 表示当前的回文子串个数为j
5: for(1<=k<=num[i]) 枚举每个长度为i的回文子串选取的个数
6: k+j<=K && l+k*i<=L 边界条件
dp[now][k+j][l+k*i] | =dp[last][k][j] 当前的回文子串个数为k+j,长度为l+k*i的状态需要从上一层的回文子 串个数为k,长度为j抑或得到
其实我现在都好不是很能理解,可能我需要磨一磨具体的多重背包的推导过程看看
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<string>#include<vector>#include <ctime>#include<queue>#include<set>#include<map>#include<stack>#include<iomanip>#include<cmath>#define mst(ss,b) memset((ss),(b),sizeof(ss))#define maxn 0x3f3f3f3f#define MAX 1000100///#pragma comment(linker, "/STACK:102400000,102400000")typedef long long ll;typedef unsigned long long ull;#define INF (1ll<<60)-1using namespace std;int n,K,L;int dp[2][110][110];int num[110];char s[110];bool pd(int l,int r){ while(l<=r){ if(s[l]==s[r]){ l++; r--; } else return false; } return true;}int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d%d%d",&n,&K,&L); mst(num,0); for(int i=1;i<=n;i++){ scanf("%s",&s); for(int j=0;j<strlen(s);j++){ for(int k=j;k<strlen(s);k++){ if(pd(j,k)) num[k-j+1]++; } } } int now=0,last=1; dp[now][0][0]=1; for(int i=1;i<=L;i++){ swap(now,last); mst(dp[now],0); for(int l=0;l<=L;l++){ for(int j=0;j<=K;j++){ for(int k=0;k<=num[i];k++){ if(k+j>K || l+k*i>L) continue; dp[now][k+j][l+k*i]|=dp[last][j][l]; } } } } if(dp[now][K][L]) printf("True\n"); else printf("False\n"); } return 0;}
0 0
- BestCoder Round #82 (div.1) 1002 HDU 5677 dp-类似多重背包的思想
- HDU 5569 matrix(DP)——BestCoder Round #63(div.1 div.2)
- Codeforces Round #360 (Div. 2) E dp 类似01背包
- Codeforces Round #302 (Div. 2) C dp(背包思想)
- HDU 5464 Clarke and problem(dp 动态规划)——BestCoder Round #56(div.1 div.2)
- BestCoder Round #68 (div.1) B 矩阵乘法优化DP
- (BestCoder Round #59 (div.1) B)简单DP
- BestCoder Round #56 1002 hdu 5464 (背包问题)
- BestCoder Round #81 (div.2)的题目 HDU5671 HDU 5670
- BestCoder Round #54 (div.2) HDU 5428 The Factor(1002)
- hdu 5495 BestCoder Round #58 (div.2) 1002
- HDU 5671 Matrix (BestCoder Round #81 (div.2) 1002)
- BestCoder Round #64 (div.1) HDU Array 乱搞
- BestCoder Round #81 (div.1) A B hdu5571 hdu 5572
- hdu 5389 dp类似背包
- BestCoder Round #82 (div.2)
- BestCoder Round #82 (div.2)
- BestCoder Round #54 (div.1)
- Xftp5中文文件乱码
- 带你系统学习GCD(二)
- python3+scapy扫描获取局域网主机ip和mac
- Notes wk10 sensor & lbs
- Android的监听滞后性
- BestCoder Round #82 (div.1) 1002 HDU 5677 dp-类似多重背包的思想
- 解决ubuntu下Android Studio出现“aapt” IOException error=2, No such file or directory问题
- 找最长公共子串的函数
- 剑指offer刷题—斐波那契数列
- Linux命令缩写的解释
- [LeetCode#4][C]Median of Two Sorted Arrays
- JAVA两个字符串截取相同的最长子字符串
- ISA TEST黑客过关小游戏第六关解密
- Fragment中的onActivityResult方法不调用