[动态规划]Letter Deletion

本文探讨了如何通过删除部分字母使两个英文单词变为相同的最长可能长度的问题,使用了一个高效算法来解决这个问题,该算法利用动态规划思想,适用于处理长度在1到200之间的单词。文章提供了一个C语言实现的示例,展示了如何通过比较和记录两个字符串中相同字符的位置,逐步构建最长公共子序列。

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

Letter Deletion


Time Limit:1sMemory limit:32M
Accepted Submit:159Total Submit:372

You are given two words (each word consists of upper-case English letters).

Try to delete some letters from each word so that the resulting words are equal.

What is the maximum possible length of the resulting word?

Input

There will be no more than 10 test cases.

Each test case consists of a single line, contaning the two words separated by a single space. The length of each of these words is between 1 and 200.

Output

For each test case output the maximum length of a resulting word (the length of the longest word that can be created from both words by removing some letters).

If the two words have no letters in common, output 0.

Sample input

AAABBB ABABAB
AXYAAZ CCCXCCCYCCCZCC
ABCDE EDCBA

Sample output

4
3
1
#include <stdio.h>
#include <math.h>
#include <stdlib.h>

#define MAX_VAL(x, y) ((x) > (y) ? (x) : (y))
#define MAX_VAL3(x, y, z) (MAX_VAL(MAX_VAL(x, y), z))

int main()
{
    int i, j;
    int len1, len2;
    char str1[300], str2[300];
    int result[300][300];

    while (scanf("%s", &str1[1]) != EOF)
    {
        scanf("%s", &str2[1]);
        len1 = strlen(&str1[1]);
        len2 = strlen(&str2[1]);

        memset(result, 0, sizeof(result));
        for (i = 1; i <= len1; ++i)
        {
            for (j = 1; j <= len2; ++j)
            {
                if (str1[i] == str2[j])
                    result[i][j] = MAX_VAL3(result[i - 1][j - 1] + 1, result[i][j - 1], result[i - 1][j]);
                else
                    result[i][j] = MAX_VAL(result[i - 1][j], result[i][j - 1]);
            }
        }

        printf("%d\n", result[len1][len2]);
    }

    return 1;
}

 

org.apache.ibatis.exceptions.PersistenceException: ### Error updating database. Cause: dm.jdbc.driver.DMException: 违反引用约束[ACT_RU_EXECUTION_IBFK_3] ### The error may involve org.activiti.engine.impl.persistence.entity.ExecutionEntityImpl.bulkInsertExecution-Inline ### The error occurred while setting parameters ### SQL: insert into ACT_RU_EXECUTION (ID_, REV_, PROC_INST_ID_, BUSINESS_KEY_, PROC_DEF_ID_, ACT_ID_, IS_ACTIVE_, IS_CONCURRENT_, IS_SCOPE_,IS_EVENT_SCOPE_, IS_MI_ROOT_, PARENT_ID_, SUPER_EXEC_, ROOT_PROC_INST_ID_, SUSPENSION_STATE_, TENANT_ID_, NAME_, START_TIME_, START_USER_ID_, IS_COUNT_ENABLED_, EVT_SUBSCR_COUNT_, TASK_COUNT_, JOB_COUNT_, TIMER_JOB_COUNT_, SUSP_JOB_COUNT_, DEADLETTER_JOB_COUNT_, VAR_COUNT_, ID_LINK_COUNT_) values (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) , (?, 1, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?) ### Cause: dm.jdbc.driver.DMException: 违反引用约束[ACT_RU_EXECUTION_IBFK_3] at org.apache.ibatis.exceptions.ExceptionFactory.wrapException(ExceptionFactory.java:30) at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:200) at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:185) at org.activiti.engine.impl.db.DbSqlSession.flushBulkInsert(DbSqlSession.java:680) at org.activiti.engine.impl.db.DbSqlSession.flushInsertEntities(DbSqlSession.java:550) at org.activiti.engine.impl.db.Db
最新发布
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值