uva 10405 Longest Common Subsequence

简介: #include<iostream>#include<cstdio>#include<cstring>using namespace std;char str1[1002],str2[1002];int d[1002][1002];int main(){ while(gets(str1) && gets(str2))
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
char str1[1002],str2[1002];
int d[1002][1002];
int main()
{
    while(gets(str1) && gets(str2))
    {
        int len1=strlen(str1),len2=strlen(str2);
        memset(d,0,sizeof(d));
        for(int i=1; i<=len1; ++i)
            for(int j=1; j<=len2; ++j)
            {
                if(str1[i-1]==str2[j-1])
                    d[i][j]=d[i-1][j-1]+1;
                else
                    d[i][j]=max(d[i-1][j],d[i][j-1]);
            }
        printf("%d\n",d[len1][len2]);
    }
    return 0;
}


模板:


目录
相关文章
Leetcode 516. Longest Palindromic Subsequence
找到一个字符串的最长回文子序列,这里注意回文子串和回文序列的区别。子序列不要求连续,子串(substring)是要求连续的。leetcode 5. Longest Palindromic Substring就是求连续子串的。
58 0
|
8月前
|
人工智能
HDU-1159-Common Subsequence
HDU-1159-Common Subsequence
46 0
|
人工智能 BI
UVA live 2678 - Subsequence
关于这个题目,有多种的解法,如果枚举起点和终点,时间复杂度为O(n^3),但如果我们用一个数组B把一段数的和存起来,B[i] = sum(a[1].....a[i])。这样就可以把时间复杂度降到O(n^2)。
85 0
|
算法
LeetCode 300. Longest Increasing Subsequence
给定一个无序的整数数组,找到其中最长上升子序列的长度。
58 0
LeetCode 300. Longest Increasing Subsequence
AtCoder Beginner Contest 214 F - Substrings(subsequence DP)
AtCoder Beginner Contest 214 F - Substrings(subsequence DP)
102 0
|
人工智能
POJ 2533 Longest Ordered Subsequence
POJ 2533 Longest Ordered Subsequence
117 0
|
Linux
[LeetCode] Longest Increasing Subsequence
A typical O(n^2) solution uses dynamic programming. Let's use lens[j] to denote the length of the LIS ending with nums[j].
856 0

热门文章

最新文章