UVa 10405 - Longest Common Subsequence 最长公共子序列模板

本文介绍了一个经典的计算机科学问题——最长公共子序列问题,并提供了一段C++代码实现。该问题涉及两个字符串作为输入,输出它们之间的最长公共子序列的长度。

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

Problem C: Longest Common Subsequence

Sequence 1:                

Sequence 2:                


Given two sequences of characters, print the length of the longest common subsequence of both sequences. For example, the longest common subsequence of the following two sequences:

abcdgh
aedfhr
is  adh  of length 3.

Input consists of pairs of lines. The first line of a pair contains the first string and the second line contains the second string. Each string is on a separate line and consists of at most 1,000 characters

For each subsequent pair of input lines, output a line containing one integer number which satisfies the criteria stated above.

Sample input

a1b2c3d4e
zz1yy2xx3ww4vv
abcdgh
aedfhr
abcdefghijklmnopqrstuvwxyz
a0b0c0d0e0f0g0h0i0j0k0l0m0n0o0p0q0r0s0t0u0v0w0x0y0z0
abcdefghijklmnzyxwvutsrqpo
opqrstuvwxyzabcdefghijklmn

Output for the sample input

4
3
26
14

-----------------------------------------------------------------

要用gets才给过,但是为什捏。。。

-----------------------------------------------------------------

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

using namespace std;

char s1[1111];
char s2[1111];
int f[1111][1111];

int main()
{
    while (gets(s1+1))
    {
        memset(f,0,sizeof(f));
        gets(s2+1);
        int len1=strlen(s1+1);
        int len2=strlen(s2+1);
        for (int i=1;i<=len1;i++)
            for (int j=1;j<=len2;j++)
                f[i][j]=max( f[i-1][j-1]+(s1[i]==s2[j]), max( f[i-1][j], f[i][j-1] ) );
        printf("%d\n",f[len1][len2]);
    }
    return 0;
}





### SDUT C语言实验中最长公共子序列 (LCS) 问题解法 #### 动态规划方法概述 动态规划(Dynamic Programming, DP)是一种常用的算法设计技术,适用于解决最优化问题。在处理字符串匹配类问题时,动态规划尤为有效。对于最长公共子序列Longest Common Subsequence, LCS)问题,其核心在于通过构建一个二维表格 `dp` 来记录两个字符串之间的状态关系。 设给定的两个字符串分别为 \( X \) 和 \( Y \),长度分别为 \( m \) 和 \( n \)。定义二维数组 `dp[i][j]` 表示字符串 \( X[1..i] \) 和 \( Y[1..j] \) 的最长公共子序列的长度,则可以得到如下转移方程: \[ dp[i][j] = \begin{cases} 0 & \text{if } i=0 \text{ or } j=0 \\ dp[i-1][j-1]+1 & \text{if } X[i]=Y[j] \\ \max(dp[i-1][j], dp[i][j-1]) & \text{otherwise } \end{cases} \] 最终的结果即为 \( dp[m][n] \)[^1]。 #### 实现代码 以下是基于上述理论实现的一个标准 C 语言程序: ```c #include <stdio.h> #include <string.h> void lcs(char *X, char *Y, int m, int n) { int dp[m+1][n+1]; // 初始化 dp 数组 for(int i = 0; i <= m; i++) { for(int j = 0; j <= n; j++) { if(i == 0 || j == 0) dp[i][j] = 0; else if(X[i-1] == Y[j-1]) dp[i][j] = dp[i-1][j-1] + 1; else dp[i][j] = (dp[i-1][j] > dp[i][j-1]) ? dp[i-1][j] : dp[i][j-1]; } } printf("Length of Longest Common Subsequence is %d\n", dp[m][n]); } int main() { char X[] = "AGGTAB"; char Y[] = "GXTXAYB"; int m = strlen(X); int n = strlen(Y); lcs(X, Y, m, n); return 0; } ``` 此代码实现了计算两字符串之间最长公共子序列的功能,并打印出该子序列的长度。 #### 复杂度分析 时间复杂度主要由嵌套循环决定,由于需要遍历整个二维表,故时间为 \( O(m \times n) \)。空间上也需要开辟大小为 \( (m+1) \times (n+1) \) 的辅助矩阵用于存储中间结果,因此空间复杂度亦为 \( O(m \times n) \)。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值