hdoj 5677 ztr loves substring 【Manacher + 多重背包】

来源:互联网 发布:美工的岗位职责是哪些 编辑:程序博客网 时间:2024/05/23 20:47

题目链接:ztr loves substring

ztr loves substring

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 94 Accepted Submission(s): 48

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

题意:ztr喜欢研究子串,今天,他有n个串
现在ztr想知道,能否从这n个串的所有回文子串中,
取出恰好k个回文串且满足这些回文串的长度之和为L
以yjqqaq为例
这个串包含的回文子串有
y,j,q,a,q,qq,qaq
所以我们可以既选qq,又选qaq

思路:先求出所有的回文子串,然后就是背包了。但是直接dp会T,用二进制优化下就可以了。
dp[i][j][k]表示前i个串选j个子串长度为k是否合法。

AC代码:

#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#define CLR(a, b) memset(a, (b), sizeof(a))using namespace std;typedef long long LL;const int MAXN = 5*1e6 +10;const int INF = 0x3f3f3f3f;char str[110*2];int p[110*2], num[110];void Manacher(char *T) {    int len = strlen(T);    int l = 0;    str[l++] = '@'; str[l++] = '#';    for(int i = 0; i < len; i++) {        str[l++] = T[i];        str[l++] = '#';    }    str[l] = 0;    int mx = 0, id = 0;    for(int i = 0; i < l; i++) {        if(mx > i) {            p[i] = min(p[2*id - i], mx-i);        }        else {            p[i] = 1;        }        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] >= 'a' && str[i] <= 'z') {            for(int j = 1; j <= p[i]-1; j += 2) num[j]++;        }        else {            for(int j = 2; j <= p[i]-1; j += 2) {                num[j]++;            }        }    }}int val[4010], cnt[4010];bool dp[110][110][110];int main(){    int t; scanf("%d", &t);    while(t--) {        int N, K, L; scanf("%d%d%d", &N, &K, &L);        CLR(num, 0);        for(int i = 1; i <= N; i++) {            char s[110]; scanf("%s", s); Manacher(s);        }//        for(int i = 1; i <= 10; i++) {//            cout << num[i] << endl;//        }        int k = 1;        for(int i = 1; i <= 100; i++) {            for(int j = 1; j <= num[i]; j <<= 1) {                val[k] = j * i;                cnt[k++] = j;                num[i] -= j;            }            if(num[i] > 0) {                val[k] = i * num[i];                cnt[k++] = num[i];            }        }        CLR(dp, false); k--;        dp[1][cnt[1]][val[1]] = true; dp[1][0][0] = true;        for(int i = 1; i < k; i++) {            for(int j = 0; j <= K; j++) {                for(int k = 0; k <= L; k++) {                    if(dp[i][j][k]) {                        if(j + cnt[i+1] <= K && k + val[i+1] <= L) {                            dp[i+1][j+cnt[i+1]][k+val[i+1]] = true;                        }                        dp[i+1][j][k] = true;                    }                }            }        }        printf(dp[k][K][L] ? "True\n" : "False\n");    }    return 0;}
1 0