#include <stdio.h> #include <string.h> int b[50][50]; int c[50][50]; int length = 0; void lcs(char *x, int m, char *y, int n) { int i; int j; for(i = 1; i <= m; i++) c[i][0] = 0; for(i = 1; i <= n; i++) c[0][i] = 0; for(i = 1; i <= m;…
先上状态转移方程,还是很容易看明白的 例题是Codevs的1862,这个题不是实现了方程就可以了的,还要完成一个事情那就是计数,数一数到底有多少个最长公共子序列 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; ; const int p=1e8; char a[maxn],b[maxn]; int dp[maxn][maxn],f[maxn][maxn]; int m…
一.Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strict…
由于动态规划的LCS问题,需要从第一个字符开始读取比较方便.所以用gets_s();第一个参数是起始位置,第二个参数是字读取字符的长度. #include<bits/stdc++.h> #include<cstdio> using namespace std; const int N = 100; char A[N], B[N]; int dp[N][N]; int main() { int n; gets_s(A + 1,105); gets_s(B + 1, 105); int…
一.最长公共子序列问题(LCS问题) 给定两个字符串A和B,长度分别为m和n,要求找出它们最长的公共子序列,并返回其长度.例如: A = "HelloWorld" B = "loop" 则A与B的最长公共子序列为 "loo",返回的长度为3.此处只给出动态规划的解法:定义子问题dp[i][j]为字符串A的第一个字符到第 i 个字符串和字符串B的第一个字符到第 j 个字符的最长公共子序列,如A为“app”,B为“apple”,dp[2][3]…
一.Description A palindrome is a symmetrical string, that is, a string read identically from left to right as well as from right to left. You are to write a program which, given a string, determines the minimal number of characters to be inserted into…
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…