#include <iostream> #include <algorithm> #include <string> #include <cstring> #include <cstdio> #define MAX 1005 using namespace std; int ans[MAX][MAX]; int main(){ string s1,s2; while(cin>>s1>>s2) { memset(ans,,s…
POJ 1458 Common Subsequence(LCS最长公共子序列)解题报告 题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=87730#problem/F 题目: Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 43388   Accepted: 17613 Description A subsequen…
POJ 1458 最长公共子序列 题目大意:给出两个字符串,求出这样的一 个最长的公共子序列的长度:子序列 中的每个字符都能在两个原串中找到, 而且每个字符的先后顺序和原串中的 先后顺序一致. Sample Input : abcfbc abfcab programming contest abcd mnp Sample Output 4 2 0 分析: 输入两个串s1,s2, 设dp(i,j)表示: s1的左边i个字符形成的子串,与s2左边的j个 字符形成的子串的最长公共子序列的长度(i,j从…
子序列就是子序列中的元素是母序列的子集,且子序列中元素的相对顺序和母序列相同. 题目要求便是寻找两个字符串的最长公共子序列. dp[i][j]表示字符串s1左i个字符和s2左j个字符的公共子序列的最大长度. 注意s1第i个字符为s1[i-1] 于是有递推公式: 对于abcfbc和abfcab两个字符串,求公共子串的最大长度的过程如图: //#define LOCAL #include <iostream> #include <cstdio> #include <cstring…
Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 22698    Accepted Submission(s): 9967 Problem Description A subsequence of a given sequence is the given sequence with some el…
经典的最长公共子序列问题. 状态转移方程为 : if(x[i] == Y[j]) dp[i, j] = dp[i - 1, j - 1] +1 else dp[i, j] = max(dp[i - 1], j, dp[i, j - 1]); 设有字符串X和字符串Y,dp[i, j]表示的是X的前i个字符与Y的前j个字符的最长公共子序列长度. 如果X[i] == Y[j] ,那么这个字符与之前的LCS 一定可以构成一个新的LCS: 如果X[i] != Y[j] ,则分别考察 dp[i  -1][j…
最长公共子序列可以用在下面的问题时:给你一个字符串,请问最少还需要添加多少个字符就可以让它编程一个回文串? 解法:ans=strlen(原串)-LCS(原串,反串); Sample Input abcfbc abfcab programming contest abcd mnp Sample Output 4 2 0 代码: #include <stdio.h> #include <string.h> #include <stdlib.h> #include <c…
题意: 两个字符串,判断最长公共子序列的长度. 思路: 直接看代码,,注意边界处理 代码: char s1[505], s2[505]; int dp[505][505]; int main(){ while(scanf("%s%s",s1,s2)!=EOF){ int l1=strlen(s1); int l2=strlen(s2); mem(dp,0); dp[0][0]=((s1[0]==s2[0])?1:0); rep(i,1,l1-1) if(s1[i]==s2[0]) dp…
Description In a few months the European Currency Union will become a reality. However, to join the club, the Maastricht criteria must be fulfilled, and this is not a trivial task for the countries (maybe except for Luxembourg). To enforce that Germa…
Common Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 34819 Accepted Submission(s): 15901 Problem Description A subsequence of a given sequence is the given sequence with some element…