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

题目链接: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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值