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))
    {
        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;
}

模板:

时间: 2024-12-23 19:06:39

uva 10405 Longest Common Subsequence的相关文章

UVa 10405 Longest Common Subsequence (DP&amp;amp;LCS)

10405 - Longest Common Subsequence Time limit: 3.000 seconds http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1346 Sequence 1: Sequence 2: Given two sequences of characters, print

UVA 10405 Longest Common Subsequence:简单DP

省赛还有不到50天了,自己DP这块实在是弱,准备就拿着些天狂刷DP了. http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1346 大意: 求两个字符串的最长公共子序列. 思路:水题,不过需要注意的就是字符串里可能会出现空格,需要用gets,真是坑. 更多精彩内容:http://www.bianceng.cnhttp://www.biancen

UVa 10405:Longest Common Subsequence,最长公共子序列模板题

[链接] http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=114&page=show_problem&problem=1346 [原题] Problem C: Longest Common Subsequence Sequence 1: Sequence 2: Given two sequences of characters, print the length of

LCS (Longest Common Subsequence) 字符串最长公共子串算法

LCS (Longest Common Subsequence) 算法用于找出两个字符串最长公共子串. 算法原理: (1) 将两个字符串分别以行和列组成矩阵.(2) 计算每个节点行列字符是否相同,如相同则为 1.(3) 通过找出值为 1 的最长对角线即可得到最长公共子串. 人 民 共 和 时 代中 0, 0, 0, 0, 0, 0华 0, 0, 0, 0, 0, 0人 1, 0, 0, 0, 0, 0民 0, 1, 0, 0, 0, 0共 0, 0, 1, 0, 0, 0和 0, 0, 0, 1

[算法]-Longest Increasing Subsequence

看微软笔试题遇到的. Longest Increasing Subsequence(LIS) means a sequence containing some elements in another sequence by the same order, and the values of elements keeps increasing.For example, LIS of {2,1,4,2,3,7,4,6} is {1,2,3,4,6}, and its LIS length is 5.

LeetCode 14 Longest Common Prefix(最长公共前缀)

翻译 写一个函数(或方法)来寻找一个字符串数组中的最长公共前缀. 原文 Write a function to find the longest common prefix string amongst an array of strings. 释义 "abcdefg" "abcdefghijk" "abcdfghijk" "abcef" 上面的字符串数组的最长公共前缀就是"abc". 思考 如下图所示,第

[LeetCode]--14. Longest Common Prefix

Write a function to find the longest common prefix string amongst an array of strings. public String longestCommonPrefix(String[] strs) { if (strs == null || strs.length == 0) return ""; String prefix = strs[0]; for (int i = 1; i < strs.lengt

[LeetCode] Longest Increasing Subsequence

Given an unsorted array of integers, find the length of longest increasing subsequence. For example, Given [10, 9, 2, 5, 3, 7, 101, 18], The longest increasing subsequence is [2, 3, 7, 101], therefore the length is 4. Note that there may be more than

POJ 2533 Longest Ordered Subsequence

Description A numeric sequence of ai is ordered if a1 < a2 < - < aN. Let the subsequence of the given numeric sequence (a1, a2, -, aN) be any sequence (ai1, ai2, -, aiK), where 1 <= i1 < i2 < - < iK <= N. For example, sequence (1,