Link: poke here
ZTR loves substringTime 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 t hese 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 is 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
Test instructions
Give n string, from the n string to select K palindrome substring, so that the length of the selected K palindrome substring of the sum of L, meet the condition output true, otherwise false
Ideas:
Number of palindrome substrings with length I (1<=i<=l) num[i]
Set DP Status: Dp[i][j][k] Indicates the current execution of I trade-offs, can reach the selection of J Palindrome substring, the length of K, can reach the state dp[i][j][k]=1, otherwise 0
Since each set of DP states is only related to the previous layer, you can use a scrolling array to eliminate the one-dimensional I
DP[2][I][J] indicates that the currently selected I palindrome string allows the state of the length j to be reached, and the current state only needs to be transferred over the previous layer.
Then we'll analyze how to implement the DP state transfer.
1:dp[now][0][0]=1
2:for (1<=I<=L) enumeration of palindrome lengths of I to calculate contribution
3:for (1<=l<=l) indicates that the current length is L
4:for (1<=j<=k) indicates that the current number of palindrome substrings is J
5: for (1<=k<=num[i]) enumerates the number of palindrome strings selected for each length I
6: k+j<=k && l+k*i<=l boundary conditions
Dp[now][k+j][l+k*i] | =DP[LAST][K][J] The current number of palindrome substring is k+j, the length of the l+k*i state needs from the previous layer of palindrome substring number k, the length of J or get
In fact, I'm all right now, not very understanding, maybe I need to grind a grinding concrete multi-backpack of the derivation process to see
Code:
#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;}
Bestcoder Round #82 (Div.1) 1002 HDU 5677 dp-thoughts like multiple backpacks