POJ 1458 Common Subsequence 最长公共子序列 LCS

LCS

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #include<iostream>
 5 #define clc(a,b) memset(a,b,sizeof(a))
 6 #define LL long long
 7 #include<cmath>
 8 using namespace std;
 9 int dp[1010][1010];//表示到i-1,j-1的最长公共长度
10 int main() {
11 //    freopen("in.txt","r",stdin);
12     char s1[1010],s2[1010];
13     while(~scanf("%s %s",s1,s2)) {
14 //        getchar();
15 //        scanf("%s",s2);
16 //        getchar();
17         int len1=strlen(s1);
18         int len2=strlen(s2);
19         clc(dp,0);
20         for(int i=1; i<=len1; i++) {
21             for(int j=1; j<=len2; j++) {
22                 if(s1[i-1]==s2[j-1]) {
23                     dp[i][j]=dp[i-1][j-1]+1;
24                 } else {
25                     dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
26                 }
27             }
28         }
29         printf("%d\n",dp[len1][len2]);
30     }
31     return 0;
32 }

时间: 2024-08-05 04:50:24

POJ 1458 Common Subsequence 最长公共子序列 LCS的相关文章

POJ 1458 Common Subsequence(最长公共子序列LCS)

POJ1458 Common Subsequence(最长公共子序列LCS) http://poj.org/problem?id=1458 题意: 给你两个字符串, 要你求出两个字符串的最长公共子序列长度. 分析: 本题不用输出子序列,非常easy,直接处理就可以. 首先令dp[i][j]==x表示A串的前i个字符和B串的前j个字符的最长公共子序列长度为x. 初始化: dp全为0. 状态转移: IfA[i]==B[j] then dp[i][j]= dp[i-1][j-1]+1 else dp[

POJ 1458 Common Subsequence 最长公共子序列

题目大意:求两个字符串的最长公共子序列 题目思路:dp[i][j] 表示第一个字符串前i位 和 第二个字符串前j位的最长公共子序列 #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<iostream> #include<algorithm> #define INF 0x3f3f3f3f #define MAXSIZE 10

POJ 1458 Common Subsequence-dp-(最长公共子序列模型)

题意:裸最长公共子序列 分析: 直接套用模型,注意初始化和实现的是细节 代码: #include<iostream> #include<string> #include<algorithm> #define max(a,b) a>b?a:b using namespace std; int dp[1010][1010]; string a,b; int main() { while(cin>>a>>b){ for(int i=0;i<

hdu 1159 Common Subsequence(最长公共子序列 DP)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 25416    Accepted Submission(s): 11276 Problem Description A subsequence of

LCS(Longest Common Subsequence 最长公共子序列)

问题描述 最长公共子序列,英文缩写为LCS(Longest Com #include <bits/stdc++.h> const int MAX=1010; char x[MAX]; char y[MAX]; int DP[MAX][MAX]; int b[MAX][MAX]; using namespace std; int PRINT_LCS(int b[][MAX],char *x,int i,int j) { if(i==0||j==0) return 1; if(b[i][j]==1

LCS修改版(Longest Common Subsequence 最长公共子序列)

题目描述 作为一名情报局特工,Nova君(2号)有着特殊的传达情报的技巧.为了避免被窃取情报,每次传达时,他都会发出两句旁人看来意义不明话,实际上暗号已经暗含其中.解密的方法很简单,分别从两句话里删掉任意多个字母,使得两句话剩余的部分相同,通过一定的删除手法,可以让剩余的部分相同且长度最大,就得到了可能的暗号.暗号可能有多个,还要进行筛选,现在情报局人手不够,希望你能助一臂之力,筛选工作不用你完成,你只需计算出暗号长度以及个数即可.(注意,字母的位置也是暗号的重要信息,位置不同的字母组成的暗号不

hdu 1159 Common Subsequence(最长公共子序列 动态规划)

Common Subsequence Problem 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

hdu 1159 Common Subsequence(最长公共子序列)

#include<bits/stdc++.h> using namespace std; int dp[1024][1024]; int main() { int i,j; char s1[1024],s2[1024]; while(~scanf("%s %s",s1,s2)) { memset(dp,0,sizeof(dp)); int len1=strlen(s1); int len2=strlen(s2); for(i=0;i<len1;i++) { for(j

Poj 1458 Common Subsequence(LCS)

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 strictly