POJ 1458 Common Subsequence - from lanshui_Yang

本文介绍了一个简单的动态规划(DP)问题,即求解两个字符串的最长公共子序列。通过阅读推荐博客,您可以了解解题思路并获取作者提供的代码实现。此过程涉及字符串比较和状态转移矩阵的应用。

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

              题目大意:给你连个字符串A 和 B , 让你求A 和 B 的最长公共子序列。

         解题思路:此题属简单的DP 问题, 具体讲解推荐以下博客:

                    http://blog.youkuaiyun.com/yysdsyl/article/details/4226630

我的代码如下:

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string>
#include<algorithm>
#include<cmath>
using namespace std ;
const int MAXN = 1e4 + 5 ;
char x[MAXN] ;
char y[MAXN] ;
int L[MAXN][MAXN] ;
void solve()
{
    int lenx = strlen(x) ;
    int leny = strlen(y) ;
    int i , j ;
    for(i = 0 ; i <= lenx ; i ++)
        L[i][0] = 0 ;
    for(j = 0 ; j <= leny ; j ++)
        L[0][j] = 0 ;
    for(i = 1 ; i <= lenx ; i ++)
    {
        for(j = 1 ; j <= leny ; j ++)
        {
            if(x[i - 1] == y[j - 1])
            {
                L[i][j] = L[i - 1][j - 1] + 1 ;
            }
            else
            {
                L[i][j] = max(L[i - 1][j] , L[i][j - 1]) ;
            }
        }
    }
    printf("%d\n" , L[lenx][leny]) ;
}
int main()
{
    while (scanf("%s" , x) != EOF)
    {
        scanf("%s" , y) ;
        solve() ;
    }
    return 0 ;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值