hdu 1243 反恐训练营 lcs

这是一个ACM竞赛题目,要求求解两个字符串的最长公共子序列(LCS)。作者提供了C++实现的LCS算法,并提到可以通过优化排除无关字符来减少循环次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>

#define inf 0x3f3f3f3f
#define LL long long
using namespace std;

/************************************************

designer:hl
time:2016/11/15
Exe.Time:780 ms
Exe.Memory:18820 KB

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1243

题意:亲切中文题

题解:LCS裸
todo:其实可以加个优化在上面 就是先把无关的子弹都排除掉。这样可以减少循环的次数

************************************************/




int dp[2100][2100];

int main() {
    int i, j, k, l, m, n;
    int score[1200];
    char type[2100], terror[2100], bullet[2100];

    while (~scanf("%d\n", &n)) {
        memset(dp, 0, sizeof(dp));
        memset(score, 0, sizeof(score));
        scanf("%s", type);
        for (i = 0; i < n; i++) {
            scanf("%d", &score[type[i]]);
        }
        scanf("%*c");
        scanf("%s", bullet);
        scanf("%s", terror);
        int bulletLen = strlen(bullet);
        int terrorLen = strlen(terror);


        for (i = 1; i <= bulletLen; i++) {
            for (j = 1; j <= terrorLen; j++) {
                if (bullet[i - 1] == terror[j - 1]) {
                    dp[i][j] = max(dp[i - 1][j - 1] + score[terror[j - 1]], dp[i][j]);
                } else {
                    dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]);
                }
            }
        }
        printf("%d\n", dp[bulletLen][terrorLen]);

    }

    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值