HDU 1159 Common Subsequence【dp+最长公共子序列】

Common Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 39559    Accepted Submission(s): 18178


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 strictly increasing sequence <i1, i2, ..., ik> of indices of X such that for all j = 1,2,...,k, xij = zj. For example, Z = <a, b, f, c> is a subsequence of X = <a, b, c, f, b, c> with index sequence <1, 2, 4, 6>. Given two sequences X and Y the problem is to find the length of the maximum-length common subsequence of X and Y.
The program input is from a text file. Each data set in the file contains two strings representing the given sequences. The sequences are separated by any number of white spaces. The input data are correct. For each set of data the program prints on the standard output the length of the maximum-length common subsequence from the beginning of a separate line.
 

 

Sample Input
abcfbc abfcab
programming contest
abcd mnp
 

 

Sample Output
4
2
0
 

 

Source
 

 

Recommend
Ignatius

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

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

思路:这题是简单的动态规划题。以题目的第一组测试数据为例。(参考A_Eagle的题解)

abcfbc abfcab。

 

辅助空间变化示意图

 


可以看出:

dp[i][j]=dp[i-1][j-1]+1;(a[i]==b[j])

dp[i][j]=max(dp[i-1][j],dp[i][j-1])(a[i]!=b[j]);

 

n由于F(i,j)只和dp(i-1,j-1), dp(i-1,j)和dp(i,j-1)有关, 而在计算dp(i,j)时, 只要选择一个合适的顺序, 就可以保证这三项都已经计算出来了, 这样就可以计算出dp(i,j). 这样一直推到dp(len(a),len(b))就得到所要求的解了.

下面给出AC代码:

复制代码
 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 inline int read()
 4 {
 5     int x=0,f=1;
 6     char ch=getchar();
 7     while(ch<'0'||ch>'9')
 8     {
 9         if(ch=='-')
10             f=-1;
11         ch=getchar();
12     }
13     while(ch>='0'&&ch<='9')
14     {
15         x=x*10+ch-'0';
16         ch=getchar();
17     }
18     return x*f;
19 }
20 inline void write(int x)
21 {
22     if(x<0)
23     {
24         putchar('-');
25         x=-x;
26     }
27     if(x>9)
28     {
29         write(x/10);
30     }
31     putchar(x%10+'0');
32 }
33 char s1[1005],s2[1005];
34 int dp[1005][1005];
35 int main()
36 {
37     int len1,len2;
38     while(scanf("%s%s",s1+1,s2+1)!=EOF)
39     {
40         memset(dp,0,sizeof(dp));
41         len1=strlen(s1+1),len2=strlen(s2+1);
42         for(int i=1;i<=len1;i++)
43         {
44             for(int j=1;j<=len2;j++)
45             {
46                 if(s1[i]==s2[j] )
47                     dp[i][j]=dp[i-1][j-1]+1;
48                 else
49                     dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
50             }
51         }
52         printf("%d\n",dp[len1][len2]);
53     }
54     return 0;
55 }
复制代码

 

posted @   Angel_Kitty  阅读(189)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示
哥伦布
14°
14:09发布
哥伦布
14:09发布
14°
大雨
南风
3级
空气质量
相对湿度
93%
今天
中雨
14°/19°
周日
中雨
5°/19°
周一
1°/11°