poj1458

 1 //Accepted    4112 KB    16 ms
 2 //最长公共子串
 3 #include <cstdio>
 4 #include <cstring>
 5 #include <iostream>
 6 using namespace std;
 7 const int imax_n = 1005;
 8 int dp[imax_n][imax_n];
 9 char s1[imax_n];
10 char s2[imax_n];
11 int len1,len2;
12 int max(int a,int b)
13 {
14     return a>b?a:b;
15 }
16 void Dp()
17 {
18     memset(dp,0,sizeof(dp));
19     for (int i=1;i<=len1;i++)
20     {
21         for (int j=1;j<=len2;j++)
22         {
23             dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
24             if (s1[i-1]==s2[j-1])
25             dp[i][j]=max(dp[i][j],dp[i-1][j-1]+1);
26         }
27     }
28     printf("%d\n",dp[len1][len2]);
29 }
30 int main()
31 {
32     while (scanf("%s%s",s1,s2)!=EOF)
33     {
34         len1=strlen(s1);
35         len2=strlen(s2);
36         Dp();
37     }
38     return 0;
39 }

poj1458,布布扣,bubuko.com

时间: 2024-10-28 09:58:54

poj1458的相关文章

poj1458——dp,lcs

poj1458——dp,lcs Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40529   Accepted: 16351 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence

poj1458 dp入门

Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 37551   Accepted: 15023 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..

hdu1159 poj1458 LCS裸题

HDU 1159 题意:找LCS 思路:裸题 n*m的写法,我的写法好像比较奇怪...用一个ci保存s2第i位可以做为s1的公共子序列的最大值,s1的每一位遍历s2,遍历的时候记录前面出现过的ci的最大值,ci一定是一个连序的上升序列,我的好像不是正经的LCS的算法,改天还是要学习一个的 AC代码: #include "iostream" #include "string.h" #include "stack" #include "qu

HDU1159 &amp;&amp; POJ1458:Common Subsequence(LCS)

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 there exists a stri

【POJ1458】Common Subsequence

Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 42100   Accepted: 16996 Description A subsequence of a given sequence is the given sequence with some elements (possible none) left out. Given a sequence X = < x1, x2, ..

poj1458 Common Subsequence(经典DP)

题目意思: http://poj.org/problem?id=1458 给出两个字符串,求出这两个字符串的最长子序列的长度,最长子序列的定义如下: Given a sequence X = < x1, x2, ..., xm > another sequence Z = < z1, z2, ..., zk > is a subsequence of X if there exists a strictly increasing sequence < i1, i2, ...,

HDU1159 &amp;&amp; POJ1458 Common Subsequence (LCS模版题)

题目大意:求出两个串的公共子序列的长度 LCS的入门题,读懂题了直接模板就可以 #include <iostream> #include <cstring> using namespace std; const int N=1000; int a[N][N]; int LCS(const char *s1, const char *s2) {// s1:0...m, s2:0...n int m = strlen(s1), n = strlen(s2); int i, j; a[0

最长公共子序列poj1458

#include<map> #include<set> #include<list> #include<cmath> #include<queue> #include<stack> #include<vector> #include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define

POJ1458 &amp;&amp; HDOJ1159 Common Subsequence【LCS】

题目链接(POJ) :http://poj.org/problem?id=1458 题目链接(HDOJ):http://acm.hdu.edu.cn/showproblem.php?pid=1159 Common Subsequence Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40156   Accepted: 16162 Description A subsequence of a given sequence