HDU 1159——Common Subsequence

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1159

题目大意:给定两个字符串,求出两个字符串最大公共字符串。

一开始的话是没有什么思路的,后来除了借鉴书上p275《算法竞赛—入门经典》和某位博主的详细讲解,才算是明白了

借鉴链接:http://blog.csdn.net/a_eagle/article/details/7213236

代码:

 1 #include<iostream>  //p275
 2 #include<cstring>
 3
 4
 5 using namespace std;
 6
 7 int f[1001][1001];//定义成全局防溢出
 8
 9 int main()
10 {
11     char a[1001],b[1001];
12     int i,j,len1,len2;
13     while(~scanf("%s %s",a,b))
14     {
15         len1=strlen(a);
16         len2=strlen(b);
17         for(i=0;i<=len1;i++)
18         {
19             f[i][0]=0;
20         }
21         for(i=0;i<=len2;i++)
22         {
23             f[0][i]=0;
24         }
25
26         for(i=1;i<=len1;i++)
27         {
28             for(j=1;j<=len2;j++)
29             {
30                 if(a[i-1]==b[j-1])
31                 {
32                     f[i][j]=f[i-1][j-1]+1;
33                 }
34                 else
35                 {
36                     f[i][j]=f[i-1][j]>f[i][j-1]?f[i-1][j]:f[i][j-1];
37                 }
38             }
39         }
40         printf("%d\n",f[len1][len2]);
41     }
42     return 0;
43 }  
时间: 2024-11-15 14:49:06

HDU 1159——Common Subsequence的相关文章

POJ 1159 Palindrome &amp;&amp; HDU 1159 Common Subsequence

1.先说说杭电的1159吧! 这道题是基础动规,比较简单! 就是要你求最长的公共子序列(不要优化) 动态转移方程: dp[i+1][j+1]=(a[i]=b[i])?dp[i][j]+1:max(dp[i+1][j],dp[i][j+1]) AC代码: #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 520 char a[N],b[N]; in

HDU 1159——Common Subsequence(DP)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 23279    Accepted Submission(s): 10242 Problem Description A subsequence of a given sequence is the given sequence with some e

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

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

题目链接: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): 37551    Accepted Submission(s): 17206 Problem Description A subsequence of

HDU 1159 Common Subsequence(裸LCS)

传送门: 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): 47676    Accepted Submission(s): 21890 Problem Description A subsequence of

hdu 1159 Common Subsequence(dp 最长公共子序列问题LCS)

最长公共子序列问题(LCS,Longerst Common Subsequence). s1s2……si+1和t1t2……tj+1的公共子序列可能是: ①当si+1=tj+1时,在s1s2……si+1和t1t2……tj+1的公共子序列末尾追加一个. ②s1s2……si+1和t1t2……tj的公共子序列 ③s1s2……si和t1t2……tj+1的公共子序列 所以易得到递推关系dp[i+1][j+1]=  max{ dp[i][j]+1 , dp[i][j+1] , dp[i+1][j]) }  

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

hdu 1159 Common Subsequence

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 24312    Accepted Submission(s): 10747 Problem Description A subsequence of a given sequence is the given sequence with some el

hdu 1159 Common Subsequence (dp求LCS)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 24489    Accepted Submission(s): 10823 Problem Description A subsequence of a given sequence is the given sequence with some e

hdu 1159 Common Subsequence(dp)

Common Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 25566    Accepted Submission(s): 11361 Problem Description A subsequence of a given sequence is the given sequence with some e